blob: 2a5c383bb859b66ff7a3e69bbae023e128012026 [file] [log] [blame]
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001//===--- Tools.cpp - Tools Implementations ------------------------------*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "Tools.h"
11
Daniel Dunbar1d460332009-03-18 10:01:51 +000012#include "clang/Driver/Action.h"
Daniel Dunbar871adcf2009-03-18 07:06:02 +000013#include "clang/Driver/Arg.h"
Daniel Dunbarb488c1d2009-03-18 08:07:30 +000014#include "clang/Driver/ArgList.h"
Daniel Dunbaree848a72009-10-29 02:39:57 +000015#include "clang/Driver/Driver.h"
16#include "clang/Driver/DriverDiagnostic.h"
Daniel Dunbar871adcf2009-03-18 07:06:02 +000017#include "clang/Driver/Compilation.h"
18#include "clang/Driver/Job.h"
Daniel Dunbarb488c1d2009-03-18 08:07:30 +000019#include "clang/Driver/HostInfo.h"
20#include "clang/Driver/Option.h"
Daniel Dunbar265e9ef2009-11-19 04:25:22 +000021#include "clang/Driver/Options.h"
Daniel Dunbarb488c1d2009-03-18 08:07:30 +000022#include "clang/Driver/ToolChain.h"
Daniel Dunbar871adcf2009-03-18 07:06:02 +000023#include "clang/Driver/Util.h"
24
Daniel Dunbar88137642009-09-09 22:32:48 +000025#include "llvm/ADT/SmallString.h"
Douglas Gregor55d3f7a2009-10-29 00:41:01 +000026#include "llvm/ADT/StringSwitch.h"
Daniel Dunbar5b750fe2009-09-09 22:32:34 +000027#include "llvm/ADT/Twine.h"
Daniel Dunbar02633b52009-03-26 16:23:12 +000028#include "llvm/Support/Format.h"
29#include "llvm/Support/raw_ostream.h"
Daniel Dunbarf86fedd2009-11-14 22:04:54 +000030#include "llvm/System/Host.h"
Daniel Dunbar55efe142009-11-04 06:24:47 +000031#include "llvm/System/Process.h"
Daniel Dunbar871adcf2009-03-18 07:06:02 +000032
33#include "InputInfo.h"
Daniel Dunbar02633b52009-03-26 16:23:12 +000034#include "ToolChains.h"
Daniel Dunbar871adcf2009-03-18 07:06:02 +000035
Daniel Dunbar47ac7d22009-03-18 06:00:36 +000036using namespace clang::driver;
37using namespace clang::driver::tools;
38
Daniel Dunbar88a3d6c2009-09-10 01:21:05 +000039/// CheckPreprocessingOptions - Perform some validation of preprocessing
40/// arguments that is shared with gcc.
41static void CheckPreprocessingOptions(const Driver &D, const ArgList &Args) {
42 if (Arg *A = Args.getLastArg(options::OPT_C, options::OPT_CC))
43 if (!Args.hasArg(options::OPT_E))
44 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
45 << A->getAsString(Args) << "-E";
46}
47
Daniel Dunbare2fd6642009-09-10 01:21:12 +000048/// CheckCodeGenerationOptions - Perform some validation of code generation
49/// arguments that is shared with gcc.
50static void CheckCodeGenerationOptions(const Driver &D, const ArgList &Args) {
51 // In gcc, only ARM checks this, but it seems reasonable to check universally.
52 if (Args.hasArg(options::OPT_static))
53 if (const Arg *A = Args.getLastArg(options::OPT_dynamic,
54 options::OPT_mdynamic_no_pic))
55 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
56 << A->getAsString(Args) << "-static";
57}
58
Chris Lattner3edbeb72010-03-29 17:55:58 +000059// Quote target names for inclusion in GNU Make dependency files.
60// Only the characters '$', '#', ' ', '\t' are quoted.
61static void QuoteTarget(llvm::StringRef Target,
62 llvm::SmallVectorImpl<char> &Res) {
63 for (unsigned i = 0, e = Target.size(); i != e; ++i) {
64 switch (Target[i]) {
65 case ' ':
66 case '\t':
67 // Escape the preceding backslashes
68 for (int j = i - 1; j >= 0 && Target[j] == '\\'; --j)
69 Res.push_back('\\');
70
71 // Escape the space/tab
72 Res.push_back('\\');
73 break;
74 case '$':
75 Res.push_back('$');
76 break;
77 case '#':
78 Res.push_back('\\');
79 break;
80 default:
81 break;
82 }
83
84 Res.push_back(Target[i]);
85 }
86}
87
Mike Stump1eb44332009-09-09 15:08:12 +000088void Clang::AddPreprocessingOptions(const Driver &D,
Douglas Gregordf91ef32009-04-18 00:34:01 +000089 const ArgList &Args,
Daniel Dunbarc21c4852009-04-08 23:54:23 +000090 ArgStringList &CmdArgs,
91 const InputInfo &Output,
92 const InputInfoList &Inputs) const {
Daniel Dunbarc21c4852009-04-08 23:54:23 +000093 Arg *A;
Daniel Dunbar3a183d32009-06-08 21:48:20 +000094
Daniel Dunbar88a3d6c2009-09-10 01:21:05 +000095 CheckPreprocessingOptions(D, Args);
96
97 Args.AddLastArg(CmdArgs, options::OPT_C);
98 Args.AddLastArg(CmdArgs, options::OPT_CC);
Daniel Dunbar3a183d32009-06-08 21:48:20 +000099
100 // Handle dependency file generation.
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000101 if ((A = Args.getLastArg(options::OPT_M)) ||
102 (A = Args.getLastArg(options::OPT_MM)) ||
103 (A = Args.getLastArg(options::OPT_MD)) ||
104 (A = Args.getLastArg(options::OPT_MMD))) {
105 // Determine the output location.
106 const char *DepFile;
107 if (Output.getType() == types::TY_Dependencies) {
108 if (Output.isPipe())
109 DepFile = "-";
110 else
111 DepFile = Output.getFilename();
112 } else if (Arg *MF = Args.getLastArg(options::OPT_MF)) {
113 DepFile = MF->getValue(Args);
Daniel Dunbarb827a052009-11-19 03:26:40 +0000114 } else if (A->getOption().matches(options::OPT_M) ||
115 A->getOption().matches(options::OPT_MM)) {
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000116 DepFile = "-";
117 } else {
118 DepFile = darwin::CC1::getDependencyFileName(Args, Inputs);
119 }
120 CmdArgs.push_back("-dependency-file");
121 CmdArgs.push_back(DepFile);
122
Chris Lattner3edbeb72010-03-29 17:55:58 +0000123 // Add a default target if one wasn't specified.
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000124 if (!Args.hasArg(options::OPT_MT) && !Args.hasArg(options::OPT_MQ)) {
125 const char *DepTarget;
126
127 // If user provided -o, that is the dependency target, except
128 // when we are only generating a dependency file.
129 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
130 if (OutputOpt && Output.getType() != types::TY_Dependencies) {
131 DepTarget = OutputOpt->getValue(Args);
132 } else {
133 // Otherwise derive from the base input.
134 //
135 // FIXME: This should use the computed output file location.
136 llvm::sys::Path P(Inputs[0].getBaseInput());
137
138 P.eraseSuffix();
139 P.appendSuffix("o");
Daniel Dunbar88137642009-09-09 22:32:48 +0000140 DepTarget = Args.MakeArgString(P.getLast());
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000141 }
142
143 CmdArgs.push_back("-MT");
Chris Lattner3edbeb72010-03-29 17:55:58 +0000144 llvm::SmallString<128> Quoted;
145 QuoteTarget(DepTarget, Quoted);
146 CmdArgs.push_back(Args.MakeArgString(Quoted));
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000147 }
148
Daniel Dunbarb827a052009-11-19 03:26:40 +0000149 if (A->getOption().matches(options::OPT_M) ||
150 A->getOption().matches(options::OPT_MD))
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000151 CmdArgs.push_back("-sys-header-deps");
152 }
153
154 Args.AddLastArg(CmdArgs, options::OPT_MP);
Chris Lattner3edbeb72010-03-29 17:55:58 +0000155
156 // Convert all -MQ <target> args to -MT <quoted target>
157 for (arg_iterator it = Args.filtered_begin(options::OPT_MT,
158 options::OPT_MQ),
159 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000160 const Arg *A = *it;
161 A->claim();
Chris Lattner3edbeb72010-03-29 17:55:58 +0000162
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000163 if (A->getOption().matches(options::OPT_MQ)) {
Chris Lattner3edbeb72010-03-29 17:55:58 +0000164 CmdArgs.push_back("-MT");
165 llvm::SmallString<128> Quoted;
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000166 QuoteTarget(A->getValue(Args), Quoted);
Chris Lattner3edbeb72010-03-29 17:55:58 +0000167 CmdArgs.push_back(Args.MakeArgString(Quoted));
168
169 // -MT flag - no change
170 } else {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000171 A->render(Args, CmdArgs);
Chris Lattner3edbeb72010-03-29 17:55:58 +0000172 }
173 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000174
Douglas Gregordf91ef32009-04-18 00:34:01 +0000175 // Add -i* options, and automatically translate to
176 // -include-pch/-include-pth for transparent PCH support. It's
177 // wonky, but we include looking for .gch so we can support seamless
178 // replacement into a build system already set up to be generating
179 // .gch files.
Daniel Dunbarcdd96862009-11-25 11:53:23 +0000180 for (arg_iterator it = Args.filtered_begin(options::OPT_clang_i_Group),
181 ie = Args.filtered_end(); it != ie; ++it) {
182 const Arg *A = it;
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000183
184 if (A->getOption().matches(options::OPT_include)) {
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000185 // Use PCH if the user requested it, except for C++ (for now).
186 bool UsePCH = D.CCCUsePCH;
187 if (types::isCXX(Inputs[0].getType()))
188 UsePCH = false;
189
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000190 bool FoundPTH = false;
Douglas Gregordf91ef32009-04-18 00:34:01 +0000191 bool FoundPCH = false;
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000192 llvm::sys::Path P(A->getValue(Args));
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000193 if (UsePCH) {
Douglas Gregordf91ef32009-04-18 00:34:01 +0000194 P.appendSuffix("pch");
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000195 if (P.exists())
Douglas Gregordf91ef32009-04-18 00:34:01 +0000196 FoundPCH = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000197 else
Douglas Gregordf91ef32009-04-18 00:34:01 +0000198 P.eraseSuffix();
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000199 }
200
Douglas Gregordf91ef32009-04-18 00:34:01 +0000201 if (!FoundPCH) {
202 P.appendSuffix("pth");
Mike Stump1eb44332009-09-09 15:08:12 +0000203 if (P.exists())
Douglas Gregordf91ef32009-04-18 00:34:01 +0000204 FoundPTH = true;
205 else
206 P.eraseSuffix();
Mike Stump1eb44332009-09-09 15:08:12 +0000207 }
208
Douglas Gregordf91ef32009-04-18 00:34:01 +0000209 if (!FoundPCH && !FoundPTH) {
210 P.appendSuffix("gch");
211 if (P.exists()) {
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000212 FoundPCH = UsePCH;
213 FoundPTH = !UsePCH;
Douglas Gregordf91ef32009-04-18 00:34:01 +0000214 }
Mike Stump1eb44332009-09-09 15:08:12 +0000215 else
Douglas Gregordf91ef32009-04-18 00:34:01 +0000216 P.eraseSuffix();
217 }
218
219 if (FoundPCH || FoundPTH) {
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000220 A->claim();
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000221 if (UsePCH)
Douglas Gregordf91ef32009-04-18 00:34:01 +0000222 CmdArgs.push_back("-include-pch");
223 else
224 CmdArgs.push_back("-include-pth");
Daniel Dunbar88137642009-09-09 22:32:48 +0000225 CmdArgs.push_back(Args.MakeArgString(P.str()));
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000226 continue;
227 }
228 }
229
230 // Not translated, render as usual.
231 A->claim();
232 A->render(Args, CmdArgs);
233 }
234
235 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U);
236 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F);
237
238 // Add -Wp, and -Xassembler if using the preprocessor.
239
240 // FIXME: There is a very unfortunate problem here, some troubled
241 // souls abuse -Wp, to pass preprocessor options in gcc syntax. To
242 // really support that we would have to parse and then translate
243 // those options. :(
244 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
245 options::OPT_Xpreprocessor);
Daniel Dunbar607d7f62009-10-29 01:53:44 +0000246
247 // -I- is a deprecated GCC feature, reject it.
248 if (Arg *A = Args.getLastArg(options::OPT_I_))
249 D.Diag(clang::diag::err_drv_I_dash_not_supported) << A->getAsString(Args);
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000250}
251
Daniel Dunbar728a5122009-09-10 06:49:20 +0000252/// getARMTargetCPU - Get the (LLVM) name of the ARM cpu we are targetting.
253//
254// FIXME: tblgen this.
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000255static const char *getARMTargetCPU(const ArgList &Args,
256 const llvm::Triple &Triple) {
Daniel Dunbar728a5122009-09-10 06:49:20 +0000257 // FIXME: Warn on inconsistent use of -mcpu and -march.
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000258
259 // If we have -mcpu=, use that.
260 if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
261 return A->getValue(Args);
262
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000263 llvm::StringRef MArch;
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000264 if (Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000265 // Otherwise, if we have -march= choose the base CPU for that arch.
266 MArch = A->getValue(Args);
267 } else {
268 // Otherwise, use the Arch from the triple.
269 MArch = Triple.getArchName();
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000270 }
271
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000272 if (MArch == "armv2" || MArch == "armv2a")
273 return "arm2";
274 if (MArch == "armv3")
275 return "arm6";
276 if (MArch == "armv3m")
277 return "arm7m";
278 if (MArch == "armv4" || MArch == "armv4t")
279 return "arm7tdmi";
280 if (MArch == "armv5" || MArch == "armv5t")
281 return "arm10tdmi";
282 if (MArch == "armv5e" || MArch == "armv5te")
283 return "arm1026ejs";
284 if (MArch == "armv5tej")
285 return "arm926ej-s";
286 if (MArch == "armv6" || MArch == "armv6k")
287 return "arm1136jf-s";
288 if (MArch == "armv6j")
289 return "arm1136j-s";
290 if (MArch == "armv6z" || MArch == "armv6zk")
291 return "arm1176jzf-s";
292 if (MArch == "armv6t2")
293 return "arm1156t2-s";
294 if (MArch == "armv7" || MArch == "armv7a" || MArch == "armv7-a")
295 return "cortex-a8";
296 if (MArch == "armv7r" || MArch == "armv7-r")
297 return "cortex-r4";
298 if (MArch == "armv7m" || MArch == "armv7-m")
299 return "cortex-m3";
300 if (MArch == "ep9312")
301 return "ep9312";
302 if (MArch == "iwmmxt")
303 return "iwmmxt";
304 if (MArch == "xscale")
305 return "xscale";
306
307 // If all else failed, return the most base CPU LLVM supports.
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000308 return "arm7tdmi";
309}
310
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000311/// getLLVMArchSuffixForARM - Get the LLVM arch name to use for a particular
Daniel Dunbar728a5122009-09-10 06:49:20 +0000312/// CPU.
313//
314// FIXME: This is redundant with -mcpu, why does LLVM use this.
315// FIXME: tblgen this, or kill it!
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000316static const char *getLLVMArchSuffixForARM(llvm::StringRef CPU) {
Daniel Dunbar728a5122009-09-10 06:49:20 +0000317 if (CPU == "arm7tdmi" || CPU == "arm7tdmi-s" || CPU == "arm710t" ||
318 CPU == "arm720t" || CPU == "arm9" || CPU == "arm9tdmi" ||
319 CPU == "arm920" || CPU == "arm920t" || CPU == "arm922t" ||
320 CPU == "arm940t" || CPU == "ep9312")
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000321 return "v4t";
Daniel Dunbar728a5122009-09-10 06:49:20 +0000322
323 if (CPU == "arm10tdmi" || CPU == "arm1020t")
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000324 return "v5";
Daniel Dunbar728a5122009-09-10 06:49:20 +0000325
326 if (CPU == "arm9e" || CPU == "arm926ej-s" || CPU == "arm946e-s" ||
327 CPU == "arm966e-s" || CPU == "arm968e-s" || CPU == "arm10e" ||
328 CPU == "arm1020e" || CPU == "arm1022e" || CPU == "xscale" ||
329 CPU == "iwmmxt")
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000330 return "v5e";
Daniel Dunbar728a5122009-09-10 06:49:20 +0000331
332 if (CPU == "arm1136j-s" || CPU == "arm1136jf-s" || CPU == "arm1176jz-s" ||
333 CPU == "arm1176jzf-s" || CPU == "mpcorenovfp" || CPU == "mpcore")
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000334 return "v6";
Daniel Dunbar728a5122009-09-10 06:49:20 +0000335
336 if (CPU == "arm1156t2-s" || CPU == "arm1156t2f-s")
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000337 return "v6t2";
Daniel Dunbar728a5122009-09-10 06:49:20 +0000338
339 if (CPU == "cortex-a8" || CPU == "cortex-a9")
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000340 return "v7";
Daniel Dunbar728a5122009-09-10 06:49:20 +0000341
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000342 return "";
Daniel Dunbar728a5122009-09-10 06:49:20 +0000343}
344
345/// getLLVMTriple - Get the LLVM triple to use for a particular toolchain, which
346/// may depend on command line arguments.
347static std::string getLLVMTriple(const ToolChain &TC, const ArgList &Args) {
348 switch (TC.getTriple().getArch()) {
349 default:
350 return TC.getTripleString();
351
352 case llvm::Triple::arm:
353 case llvm::Triple::thumb: {
Daniel Dunbarf4aa4f612009-09-11 01:14:50 +0000354 // FIXME: Factor into subclasses.
Daniel Dunbar728a5122009-09-10 06:49:20 +0000355 llvm::Triple Triple = TC.getTriple();
Daniel Dunbarf4aa4f612009-09-11 01:14:50 +0000356
357 // Thumb2 is the default for V7 on Darwin.
358 //
359 // FIXME: Thumb should just be another -target-feaure, not in the triple.
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000360 llvm::StringRef Suffix =
361 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
Daniel Dunbarf4aa4f612009-09-11 01:14:50 +0000362 bool ThumbDefault =
363 (Suffix == "v7" && TC.getTriple().getOS() == llvm::Triple::Darwin);
Daniel Dunbar1d65e4b2009-09-10 22:59:51 +0000364 std::string ArchName = "arm";
Daniel Dunbarf4aa4f612009-09-11 01:14:50 +0000365 if (Args.hasFlag(options::OPT_mthumb, options::OPT_mno_thumb, ThumbDefault))
366 ArchName = "thumb";
367 Triple.setArchName(ArchName + Suffix.str());
368
Daniel Dunbar728a5122009-09-10 06:49:20 +0000369 return Triple.getTriple();
370 }
371 }
372}
373
Daniel Dunbar1f95e652009-11-17 06:37:03 +0000374// FIXME: Move to target hook.
375static bool isSignedCharDefault(const llvm::Triple &Triple) {
376 switch (Triple.getArch()) {
377 default:
378 return true;
379
380 case llvm::Triple::ppc:
381 case llvm::Triple::ppc64:
382 if (Triple.getOS() == llvm::Triple::Darwin)
383 return true;
384 return false;
385
386 case llvm::Triple::systemz:
387 return false;
388 }
389}
390
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000391void Clang::AddARMTargetArgs(const ArgList &Args,
392 ArgStringList &CmdArgs) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +0000393 const Driver &D = getToolChain().getDriver();
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000394 llvm::Triple Triple = getToolChain().getTriple();
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000395
Daniel Dunbar2030d8f2009-09-14 00:34:46 +0000396 // Select the ABI to use.
397 //
398 // FIXME: Support -meabi.
399 const char *ABIName = 0;
400 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
401 ABIName = A->getValue(Args);
402 } else {
403 // Select the default based on the platform.
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000404 llvm::StringRef env = Triple.getEnvironmentName();
Rafael Espindola1ed1a592010-06-16 19:01:17 +0000405 if (env == "gnueabi")
Daniel Dunbar2030d8f2009-09-14 00:34:46 +0000406 ABIName = "aapcs-linux";
Rafael Espindola1ed1a592010-06-16 19:01:17 +0000407 else if (env == "eabi")
408 ABIName = "aapcs";
409 else
410 ABIName = "apcs-gnu";
Daniel Dunbar2030d8f2009-09-14 00:34:46 +0000411 }
412 CmdArgs.push_back("-target-abi");
413 CmdArgs.push_back(ABIName);
414
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000415 // Set the CPU based on -march= and -mcpu=.
Daniel Dunbar38b48af2009-12-18 06:30:12 +0000416 CmdArgs.push_back("-target-cpu");
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000417 CmdArgs.push_back(getARMTargetCPU(Args, Triple));
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000418
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000419 // Select the float ABI as determined by -msoft-float, -mhard-float, and
420 // -mfloat-abi=.
421 llvm::StringRef FloatABI;
422 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
423 options::OPT_mhard_float,
424 options::OPT_mfloat_abi_EQ)) {
425 if (A->getOption().matches(options::OPT_msoft_float))
426 FloatABI = "soft";
427 else if (A->getOption().matches(options::OPT_mhard_float))
428 FloatABI = "hard";
429 else {
430 FloatABI = A->getValue(Args);
431 if (FloatABI != "soft" && FloatABI != "softfp" && FloatABI != "hard") {
432 D.Diag(clang::diag::err_drv_invalid_mfloat_abi)
433 << A->getAsString(Args);
434 FloatABI = "soft";
435 }
436 }
437 }
438
439 // If unspecified, choose the default based on the platform.
440 if (FloatABI.empty()) {
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000441 const llvm::Triple &Triple = getToolChain().getTriple();
442 switch (Triple.getOS()) {
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000443 case llvm::Triple::Darwin: {
444 // Darwin defaults to "softfp" for v6 and v7.
445 //
446 // FIXME: Factor out an ARM class so we can cache the arch somewhere.
Rafael Espindolabcd6df62010-06-28 17:18:09 +0000447 llvm::StringRef ArchName =
448 getLLVMArchSuffixForARM(getARMTargetCPU(Args, Triple));
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000449 if (ArchName.startswith("v6") || ArchName.startswith("v7"))
450 FloatABI = "softfp";
451 else
452 FloatABI = "soft";
453 break;
454 }
455
Rafael Espindola7e9cebf2010-06-27 18:29:21 +0000456 case llvm::Triple::Linux: {
457 llvm::StringRef Env = getToolChain().getTriple().getEnvironmentName();
458 if (Env == "gnueabi") {
459 FloatABI = "softfp";
460 break;
461 }
462 }
463 // fall through
464
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000465 default:
466 // Assume "soft", but warn the user we are guessing.
467 FloatABI = "soft";
468 D.Diag(clang::diag::warn_drv_assuming_mfloat_abi_is) << "soft";
469 break;
470 }
471 }
472
473 if (FloatABI == "soft") {
474 // Floating point operations and argument passing are soft.
475 //
476 // FIXME: This changes CPP defines, we need -target-soft-float.
Daniel Dunbar3b315262009-11-30 08:42:00 +0000477 CmdArgs.push_back("-msoft-float");
Daniel Dunbar87667aa2009-12-08 19:49:51 +0000478 CmdArgs.push_back("-mfloat-abi");
479 CmdArgs.push_back("soft");
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000480 } else if (FloatABI == "softfp") {
481 // Floating point operations are hard, but argument passing is soft.
Daniel Dunbar87667aa2009-12-08 19:49:51 +0000482 CmdArgs.push_back("-mfloat-abi");
483 CmdArgs.push_back("soft");
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000484 } else {
485 // Floating point operations and argument passing are hard.
486 assert(FloatABI == "hard" && "Invalid float abi!");
Daniel Dunbar87667aa2009-12-08 19:49:51 +0000487 CmdArgs.push_back("-mfloat-abi");
488 CmdArgs.push_back("hard");
Daniel Dunbarcbd19332009-09-10 23:00:09 +0000489 }
Daniel Dunbar97f52ac2009-12-19 04:15:38 +0000490
491 // Set appropriate target features for floating point mode.
492 //
493 // FIXME: Note, this is a hack, the LLVM backend doesn't actually use these
494 // yet (it uses the -mfloat-abi and -msoft-float options above), and it is
495 // stripped out by the ARM target.
496
497 // Use software floating point operations?
498 if (FloatABI == "soft") {
499 CmdArgs.push_back("-target-feature");
500 CmdArgs.push_back("+soft-float");
501 }
502
503 // Use software floating point argument passing?
504 if (FloatABI != "hard") {
505 CmdArgs.push_back("-target-feature");
506 CmdArgs.push_back("+soft-float-abi");
507 }
Daniel Dunbara91320b2009-12-21 23:28:17 +0000508
509 // Honor -mfpu=.
510 //
511 // FIXME: Centralize feature selection, defaulting shouldn't be also in the
512 // frontend target.
513 if (const Arg *A = Args.getLastArg(options::OPT_mfpu_EQ)) {
514 llvm::StringRef FPU = A->getValue(Args);
515
516 // Set the target features based on the FPU.
517 if (FPU == "fpa" || FPU == "fpe2" || FPU == "fpe3" || FPU == "maverick") {
518 // Disable any default FPU support.
519 CmdArgs.push_back("-target-feature");
520 CmdArgs.push_back("-vfp2");
521 CmdArgs.push_back("-target-feature");
522 CmdArgs.push_back("-vfp3");
523 CmdArgs.push_back("-target-feature");
524 CmdArgs.push_back("-neon");
525 } else if (FPU == "vfp") {
526 CmdArgs.push_back("-target-feature");
527 CmdArgs.push_back("+vfp2");
528 } else if (FPU == "vfp3") {
529 CmdArgs.push_back("-target-feature");
530 CmdArgs.push_back("+vfp3");
531 } else if (FPU == "neon") {
532 CmdArgs.push_back("-target-feature");
533 CmdArgs.push_back("+neon");
534 } else
535 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
536 }
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000537}
538
Eric Christophered734732010-03-02 02:41:08 +0000539void Clang::AddMIPSTargetArgs(const ArgList &Args,
540 ArgStringList &CmdArgs) const {
541 const Driver &D = getToolChain().getDriver();
542
543 // Select the ABI to use.
544 const char *ABIName = 0;
545 if (Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
546 ABIName = A->getValue(Args);
547 } else {
548 ABIName = "o32";
549 }
550
551 CmdArgs.push_back("-target-abi");
552 CmdArgs.push_back(ABIName);
553
554 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
555 llvm::StringRef MArch = A->getValue(Args);
556 CmdArgs.push_back("-target-cpu");
557
558 if ((MArch == "r2000") || (MArch == "r3000"))
559 CmdArgs.push_back("mips1");
560 else if (MArch == "r6000")
561 CmdArgs.push_back("mips2");
562 else
563 CmdArgs.push_back(MArch.str().c_str());
564 }
565
566 // Select the float ABI as determined by -msoft-float, -mhard-float, and
567 llvm::StringRef FloatABI;
568 if (Arg *A = Args.getLastArg(options::OPT_msoft_float,
569 options::OPT_mhard_float)) {
570 if (A->getOption().matches(options::OPT_msoft_float))
571 FloatABI = "soft";
572 else if (A->getOption().matches(options::OPT_mhard_float))
573 FloatABI = "hard";
574 }
575
576 // If unspecified, choose the default based on the platform.
577 if (FloatABI.empty()) {
Benjamin Kramer71c972a2010-04-08 15:44:22 +0000578 // Assume "soft", but warn the user we are guessing.
579 FloatABI = "soft";
580 D.Diag(clang::diag::warn_drv_assuming_mfloat_abi_is) << "soft";
Eric Christophered734732010-03-02 02:41:08 +0000581 }
582
583 if (FloatABI == "soft") {
584 // Floating point operations and argument passing are soft.
585 //
586 // FIXME: This changes CPP defines, we need -target-soft-float.
587 CmdArgs.push_back("-msoft-float");
588 } else {
589 assert(FloatABI == "hard" && "Invalid float abi!");
590 CmdArgs.push_back("-mhard-float");
591 }
592}
593
Daniel Dunbar6acda162009-09-09 22:33:08 +0000594void Clang::AddX86TargetArgs(const ArgList &Args,
595 ArgStringList &CmdArgs) const {
Daniel Dunbare6ad3f92009-09-10 22:59:57 +0000596 if (!Args.hasFlag(options::OPT_mred_zone,
597 options::OPT_mno_red_zone,
598 true) ||
599 Args.hasArg(options::OPT_mkernel) ||
600 Args.hasArg(options::OPT_fapple_kext))
Daniel Dunbar66861e02009-11-20 22:21:36 +0000601 CmdArgs.push_back("-disable-red-zone");
Daniel Dunbare6ad3f92009-09-10 22:59:57 +0000602
Daniel Dunbare6ad3f92009-09-10 22:59:57 +0000603 if (Args.hasFlag(options::OPT_msoft_float,
604 options::OPT_mno_soft_float,
605 false))
Daniel Dunbar66861e02009-11-20 22:21:36 +0000606 CmdArgs.push_back("-no-implicit-float");
Daniel Dunbare6ad3f92009-09-10 22:59:57 +0000607
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000608 const char *CPUName = 0;
Daniel Dunbar6acda162009-09-09 22:33:08 +0000609 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000610 if (llvm::StringRef(A->getValue(Args)) == "native") {
611 // FIXME: Reject attempts to use -march=native unless the target matches
612 // the host.
613 //
614 // FIXME: We should also incorporate the detected target features for use
615 // with -native.
616 std::string CPU = llvm::sys::getHostCPUName();
617 if (!CPU.empty())
618 CPUName = Args.MakeArgString(CPU);
619 } else
620 CPUName = A->getValue(Args);
621 }
Daniel Dunbar6acda162009-09-09 22:33:08 +0000622
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000623 // Select the default CPU if none was given (or detection failed).
624 if (!CPUName) {
Daniel Dunbar6acda162009-09-09 22:33:08 +0000625 // FIXME: Need target hooks.
Benjamin Kramerf22d1fd2010-01-30 15:01:47 +0000626 if (getToolChain().getOS().startswith("darwin")) {
Daniel Dunbar6acda162009-09-09 22:33:08 +0000627 if (getToolChain().getArchName() == "x86_64")
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000628 CPUName = "core2";
Daniel Dunbar6acda162009-09-09 22:33:08 +0000629 else if (getToolChain().getArchName() == "i386")
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000630 CPUName = "yonah";
Chris Lattner86ed3a32010-04-11 19:29:39 +0000631 } else if (getToolChain().getOS().startswith("haiku")) {
632 if (getToolChain().getArchName() == "x86_64")
633 CPUName = "x86-64";
634 else if (getToolChain().getArchName() == "i386")
635 CPUName = "i586";
Daniel Dunbar6acda162009-09-09 22:33:08 +0000636 } else {
637 if (getToolChain().getArchName() == "x86_64")
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000638 CPUName = "x86-64";
Daniel Dunbar6acda162009-09-09 22:33:08 +0000639 else if (getToolChain().getArchName() == "i386")
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000640 CPUName = "pentium4";
Daniel Dunbar6acda162009-09-09 22:33:08 +0000641 }
642 }
643
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000644 if (CPUName) {
Daniel Dunbar38b48af2009-12-18 06:30:12 +0000645 CmdArgs.push_back("-target-cpu");
Daniel Dunbarf86fedd2009-11-14 22:04:54 +0000646 CmdArgs.push_back(CPUName);
647 }
648
Daniel Dunbarcdd96862009-11-25 11:53:23 +0000649 for (arg_iterator it = Args.filtered_begin(options::OPT_m_x86_Features_Group),
650 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +0000651 llvm::StringRef Name = (*it)->getOption().getName();
652 (*it)->claim();
Daniel Dunbar6acda162009-09-09 22:33:08 +0000653
Daniel Dunbarcdd96862009-11-25 11:53:23 +0000654 // Skip over "-m".
655 assert(Name.startswith("-m") && "Invalid feature name.");
656 Name = Name.substr(2);
Daniel Dunbar6acda162009-09-09 22:33:08 +0000657
Daniel Dunbarcdd96862009-11-25 11:53:23 +0000658 bool IsNegative = Name.startswith("no-");
659 if (IsNegative)
660 Name = Name.substr(3);
Daniel Dunbar6acda162009-09-09 22:33:08 +0000661
Daniel Dunbarcdd96862009-11-25 11:53:23 +0000662 CmdArgs.push_back("-target-feature");
663 CmdArgs.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name));
Daniel Dunbar6acda162009-09-09 22:33:08 +0000664 }
665}
666
Fariborz Jahanian85caf032009-10-01 20:30:46 +0000667static bool needsExceptions(const ArgList &Args, types::ID InputType,
668 const llvm::Triple &Triple) {
Rafael Espindolaf759df02009-10-01 13:33:33 +0000669 if (Arg *A = Args.getLastArg(options::OPT_fexceptions,
670 options::OPT_fno_exceptions)) {
671 if (A->getOption().matches(options::OPT_fexceptions))
672 return true;
673 else
674 return false;
675 }
676 switch (InputType) {
677 case types::TY_CXX: case types::TY_CXXHeader:
678 case types::TY_PP_CXX: case types::TY_PP_CXXHeader:
679 case types::TY_ObjCXX: case types::TY_ObjCXXHeader:
680 case types::TY_PP_ObjCXX: case types::TY_PP_ObjCXXHeader:
681 return true;
Fariborz Jahanian85caf032009-10-01 20:30:46 +0000682
Rafael Espindolaf759df02009-10-01 13:33:33 +0000683 case types::TY_ObjC: case types::TY_ObjCHeader:
684 case types::TY_PP_ObjC: case types::TY_PP_ObjCHeader:
685 if (Args.hasArg(options::OPT_fobjc_nonfragile_abi))
686 return true;
Fariborz Jahanian85caf032009-10-01 20:30:46 +0000687 if (Triple.getOS() != llvm::Triple::Darwin)
Rafael Espindolaf759df02009-10-01 13:33:33 +0000688 return false;
Fariborz Jahanian85caf032009-10-01 20:30:46 +0000689 return (Triple.getDarwinMajorNumber() >= 9 &&
690 Triple.getArch() == llvm::Triple::x86_64);
691
Rafael Espindolaf759df02009-10-01 13:33:33 +0000692 default:
693 return false;
694 }
695}
696
Daniel Dunbardd4fe002009-10-30 18:12:20 +0000697/// getEffectiveClangTriple - Get the "effective" target triple, which is the
698/// triple for the target but with the OS version potentially modified for
699/// Darwin's -mmacosx-version-min.
700static std::string getEffectiveClangTriple(const Driver &D,
701 const ToolChain &TC,
702 const ArgList &Args) {
703 llvm::Triple Triple(getLLVMTriple(TC, Args));
704
Daniel Dunbar408fced2010-01-27 00:56:49 +0000705 // Handle -mmacosx-version-min and -miphoneos-version-min.
Daniel Dunbardd4fe002009-10-30 18:12:20 +0000706 if (Triple.getOS() != llvm::Triple::Darwin) {
707 // Diagnose use of -mmacosx-version-min and -miphoneos-version-min on
708 // non-Darwin.
Daniel Dunbar408fced2010-01-27 00:56:49 +0000709 if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ,
710 options::OPT_miphoneos_version_min_EQ))
711 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbar21ae3192010-01-26 01:44:04 +0000712 } else {
Daniel Dunbar408fced2010-01-27 00:56:49 +0000713 const toolchains::Darwin &DarwinTC(
714 reinterpret_cast<const toolchains::Darwin&>(TC));
Daniel Dunbar03d87ee2010-03-20 00:50:21 +0000715
716 // If the target isn't initialized (e.g., an unknown Darwin platform, return
717 // the default triple).
718 if (!DarwinTC.isTargetInitialized())
719 return Triple.getTriple();
720
Daniel Dunbar408fced2010-01-27 00:56:49 +0000721 unsigned Version[3];
722 DarwinTC.getTargetVersion(Version);
Daniel Dunbardd4fe002009-10-30 18:12:20 +0000723
Daniel Dunbar21ae3192010-01-26 01:44:04 +0000724 // Mangle the target version into the OS triple component. For historical
725 // reasons that make little sense, the version passed here is the "darwin"
726 // version, which drops the 10 and offsets by 4. See inverse code when
727 // setting the OS version preprocessor define.
Daniel Dunbar408fced2010-01-27 00:56:49 +0000728 if (!DarwinTC.isTargetIPhoneOS()) {
729 Version[0] = Version[1] + 4;
730 Version[1] = Version[2];
731 Version[2] = 0;
732 } else {
733 // Use the environment to communicate that we are targetting iPhoneOS.
734 Triple.setEnvironmentName("iphoneos");
Daniel Dunbar21ae3192010-01-26 01:44:04 +0000735 }
736
Daniel Dunbardd4fe002009-10-30 18:12:20 +0000737 llvm::SmallString<16> Str;
Daniel Dunbar408fced2010-01-27 00:56:49 +0000738 llvm::raw_svector_ostream(Str) << "darwin" << Version[0]
739 << "." << Version[1] << "." << Version[2];
Daniel Dunbardd4fe002009-10-30 18:12:20 +0000740 Triple.setOSName(Str.str());
741 }
742
743 return Triple.getTriple();
744}
745
Daniel Dunbar47ac7d22009-03-18 06:00:36 +0000746void Clang::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar871adcf2009-03-18 07:06:02 +0000747 Job &Dest,
748 const InputInfo &Output,
Daniel Dunbar62cf6012009-03-18 06:07:59 +0000749 const InputInfoList &Inputs,
Daniel Dunbar1d460332009-03-18 10:01:51 +0000750 const ArgList &Args,
Daniel Dunbar47ac7d22009-03-18 06:00:36 +0000751 const char *LinkingOutput) const {
Daniel Dunbar0a80ba72010-03-20 04:52:14 +0000752 bool KernelOrKext = Args.hasArg(options::OPT_mkernel,
753 options::OPT_fapple_kext);
Daniel Dunbaree788e72009-12-21 18:54:17 +0000754 const Driver &D = getToolChain().getDriver();
Daniel Dunbarb488c1d2009-03-18 08:07:30 +0000755 ArgStringList CmdArgs;
756
Daniel Dunbar077ba6a2009-03-31 20:53:55 +0000757 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
758
Daniel Dunbar8ff5b282009-12-11 23:00:49 +0000759 // Invoke ourselves in -cc1 mode.
760 //
761 // FIXME: Implement custom jobs for internal actions.
762 CmdArgs.push_back("-cc1");
763
Daniel Dunbardd4fe002009-10-30 18:12:20 +0000764 // Add the "effective" target triple.
Daniel Dunbaraf07f932009-03-31 17:35:15 +0000765 CmdArgs.push_back("-triple");
Daniel Dunbardd4fe002009-10-30 18:12:20 +0000766 std::string TripleStr = getEffectiveClangTriple(D, getToolChain(), Args);
767 CmdArgs.push_back(Args.MakeArgString(TripleStr));
Daniel Dunbar728a5122009-09-10 06:49:20 +0000768
Daniel Dunbardd4fe002009-10-30 18:12:20 +0000769 // Select the appropriate action.
Daniel Dunbar1d460332009-03-18 10:01:51 +0000770 if (isa<AnalyzeJobAction>(JA)) {
771 assert(JA.getType() == types::TY_Plist && "Invalid output type.");
772 CmdArgs.push_back("-analyze");
773 } else if (isa<PreprocessJobAction>(JA)) {
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +0000774 if (Output.getType() == types::TY_Dependencies)
775 CmdArgs.push_back("-Eonly");
776 else
777 CmdArgs.push_back("-E");
Daniel Dunbar8767cbc2010-02-03 03:07:56 +0000778 } else if (isa<AssembleJobAction>(JA)) {
779 CmdArgs.push_back("-emit-obj");
Daniel Dunbar99298002010-05-27 06:18:05 +0000780
781 // At -O0, we use -mrelax-all by default.
782 bool IsOpt = false;
783 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
784 IsOpt = !A->getOption().matches(options::OPT_O0);
785 if (Args.hasFlag(options::OPT_mrelax_all,
786 options::OPT_mno_relax_all,
787 !IsOpt))
788 CmdArgs.push_back("-mrelax-all");
Daniel Dunbar1d460332009-03-18 10:01:51 +0000789 } else if (isa<PrecompileJobAction>(JA)) {
Daniel Dunbar0ebd9322009-10-15 20:02:44 +0000790 // Use PCH if the user requested it, except for C++ (for now).
791 bool UsePCH = D.CCCUsePCH;
792 if (types::isCXX(Inputs[0].getType()))
793 UsePCH = false;
794
795 if (UsePCH)
Douglas Gregordf91ef32009-04-18 00:34:01 +0000796 CmdArgs.push_back("-emit-pch");
797 else
798 CmdArgs.push_back("-emit-pth");
Daniel Dunbar1d460332009-03-18 10:01:51 +0000799 } else {
800 assert(isa<CompileJobAction>(JA) && "Invalid action for clang tool.");
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000801
Daniel Dunbar1d460332009-03-18 10:01:51 +0000802 if (JA.getType() == types::TY_Nothing) {
803 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar6c6424b2010-06-07 23:28:45 +0000804 } else if (JA.getType() == types::TY_LLVM_IR ||
805 JA.getType() == types::TY_LTO_IR) {
Daniel Dunbar1d460332009-03-18 10:01:51 +0000806 CmdArgs.push_back("-emit-llvm");
Daniel Dunbar6c6424b2010-06-07 23:28:45 +0000807 } else if (JA.getType() == types::TY_LLVM_BC ||
808 JA.getType() == types::TY_LTO_BC) {
Daniel Dunbar1d460332009-03-18 10:01:51 +0000809 CmdArgs.push_back("-emit-llvm-bc");
810 } else if (JA.getType() == types::TY_PP_Asm) {
Daniel Dunbare3b8d072009-09-17 00:47:53 +0000811 CmdArgs.push_back("-S");
Daniel Dunbar5915fbf2009-09-01 16:57:46 +0000812 } else if (JA.getType() == types::TY_AST) {
813 CmdArgs.push_back("-emit-pch");
Daniel Dunbar64952502010-02-11 03:16:21 +0000814 } else if (JA.getType() == types::TY_RewrittenObjC) {
815 CmdArgs.push_back("-rewrite-objc");
816 } else {
817 assert(JA.getType() == types::TY_PP_Asm &&
818 "Unexpected output type!");
Daniel Dunbar1d460332009-03-18 10:01:51 +0000819 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +0000820 }
821
Daniel Dunbar1d460332009-03-18 10:01:51 +0000822 // The make clang go fast button.
823 CmdArgs.push_back("-disable-free");
824
John McCallb689afb2010-02-13 03:50:24 +0000825 // Disable the verification pass in -asserts builds.
826#ifdef NDEBUG
827 CmdArgs.push_back("-disable-llvm-verifier");
828#endif
829
Daniel Dunbarc9abc042009-04-08 05:11:16 +0000830 // Set the main file name, so that debug info works even with
831 // -save-temps.
832 CmdArgs.push_back("-main-file-name");
833 CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
834
Daniel Dunbar3bbc7532009-04-08 18:03:55 +0000835 // Some flags which affect the language (via preprocessor
836 // defines). See darwin::CC1::AddCPPArgs.
837 if (Args.hasArg(options::OPT_static))
838 CmdArgs.push_back("-static-define");
839
Daniel Dunbar1d460332009-03-18 10:01:51 +0000840 if (isa<AnalyzeJobAction>(JA)) {
Ted Kremenekb8bb3e72009-09-25 05:55:59 +0000841 // Enable region store model by default.
842 CmdArgs.push_back("-analyzer-store=region");
843
Ted Kremenekb40d06d2009-12-07 22:26:14 +0000844 // Treat blocks as analysis entry points.
845 CmdArgs.push_back("-analyzer-opt-analyze-nested-blocks");
846
Daniel Dunbar1d460332009-03-18 10:01:51 +0000847 // Add default argument set.
Daniel Dunbard8fc0f22009-05-22 00:38:15 +0000848 if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
Ted Kremenekcd9902b2010-02-05 01:52:40 +0000849 CmdArgs.push_back("-analyzer-check-dead-stores");
Ted Kremenek647aa462010-04-29 22:50:22 +0000850 // Do not enable the security-syntatic check since it
851 // it needs to be refined (known issues).
852 // CmdArgs.push_back("-analyzer-check-security-syntactic");
Ted Kremenek565e4652010-02-05 02:06:54 +0000853 CmdArgs.push_back("-analyzer-check-objc-mem");
Daniel Dunbard8fc0f22009-05-22 00:38:15 +0000854 CmdArgs.push_back("-analyzer-eagerly-assume");
Ted Kremenekfa15be42010-02-05 01:57:44 +0000855 CmdArgs.push_back("-analyzer-check-objc-methodsigs");
Daniel Dunbard8fc0f22009-05-22 00:38:15 +0000856 // Do not enable the missing -dealloc check.
Ted Kremenek7909fc82010-02-05 01:59:21 +0000857 // '-analyzer-check-objc-missing-dealloc',
Ted Kremenek2ade5362010-02-05 01:55:01 +0000858 CmdArgs.push_back("-analyzer-check-objc-unused-ivars");
Daniel Dunbard8fc0f22009-05-22 00:38:15 +0000859 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000860
Daniel Dunbard8fc0f22009-05-22 00:38:15 +0000861 // Set the output format. The default is plist, for (lame) historical
862 // reasons.
863 CmdArgs.push_back("-analyzer-output");
864 if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
865 CmdArgs.push_back(A->getValue(Args));
866 else
867 CmdArgs.push_back("plist");
Daniel Dunbar1d460332009-03-18 10:01:51 +0000868
Ted Kremenek0647a7b2010-03-22 22:32:05 +0000869 // Disable the presentation of standard compiler warnings when
870 // using --analyze. We only want to show static analyzer diagnostics
871 // or frontend errors.
872 CmdArgs.push_back("-w");
873
Daniel Dunbar1d460332009-03-18 10:01:51 +0000874 // Add -Xanalyzer arguments when running as analyzer.
875 Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
Mike Stump1eb44332009-09-09 15:08:12 +0000876 }
877
Daniel Dunbare2fd6642009-09-10 01:21:12 +0000878 CheckCodeGenerationOptions(D, Args);
879
Daniel Dunbarbc85be82009-04-29 18:32:25 +0000880 // Perform argument translation for LLVM backend. This
881 // takes some care in reconciling with llvm-gcc. The
882 // issue is that llvm-gcc translates these options based on
883 // the values in cc1, whereas we are processing based on
884 // the driver arguments.
Daniel Dunbarc21c4852009-04-08 23:54:23 +0000885
Daniel Dunbarbc85be82009-04-29 18:32:25 +0000886 // This comes from the default translation the driver + cc1
887 // would do to enable flag_pic.
888 //
889 // FIXME: Centralize this code.
890 bool PICEnabled = (Args.hasArg(options::OPT_fPIC) ||
891 Args.hasArg(options::OPT_fpic) ||
892 Args.hasArg(options::OPT_fPIE) ||
893 Args.hasArg(options::OPT_fpie));
894 bool PICDisabled = (Args.hasArg(options::OPT_mkernel) ||
895 Args.hasArg(options::OPT_static));
896 const char *Model = getToolChain().GetForcedPicModel();
897 if (!Model) {
898 if (Args.hasArg(options::OPT_mdynamic_no_pic))
899 Model = "dynamic-no-pic";
900 else if (PICDisabled)
901 Model = "static";
902 else if (PICEnabled)
903 Model = "pic";
Daniel Dunbar1d460332009-03-18 10:01:51 +0000904 else
Daniel Dunbarbc85be82009-04-29 18:32:25 +0000905 Model = getToolChain().GetDefaultRelocationModel();
Daniel Dunbar1d460332009-03-18 10:01:51 +0000906 }
Daniel Dunbarf219e7c2009-11-29 07:18:39 +0000907 if (llvm::StringRef(Model) != "pic") {
908 CmdArgs.push_back("-mrelocation-model");
909 CmdArgs.push_back(Model);
910 }
Daniel Dunbarbc85be82009-04-29 18:32:25 +0000911
912 // Infer the __PIC__ value.
913 //
914 // FIXME: This isn't quite right on Darwin, which always sets
915 // __PIC__=2.
916 if (strcmp(Model, "pic") == 0 || strcmp(Model, "dynamic-no-pic") == 0) {
Daniel Dunbar76743522009-11-29 02:39:08 +0000917 CmdArgs.push_back("-pic-level");
918 CmdArgs.push_back(Args.hasArg(options::OPT_fPIC) ? "2" : "1");
Daniel Dunbarbc85be82009-04-29 18:32:25 +0000919 }
Tanya Lattner59876c22009-11-04 01:18:09 +0000920 if (!Args.hasFlag(options::OPT_fmerge_all_constants,
921 options::OPT_fno_merge_all_constants))
Daniel Dunbar66861e02009-11-20 22:21:36 +0000922 CmdArgs.push_back("-no-merge-all-constants");
Daniel Dunbar6bea73b2009-09-16 06:17:29 +0000923
Daniel Dunbarf219e7c2009-11-29 07:18:39 +0000924 // LLVM Code Generator Options.
925
926 // FIXME: Set --enable-unsafe-fp-math.
927 if (Args.hasFlag(options::OPT_fno_omit_frame_pointer,
928 options::OPT_fomit_frame_pointer))
929 CmdArgs.push_back("-mdisable-fp-elim");
930 if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
931 options::OPT_fno_zero_initialized_in_bss))
932 CmdArgs.push_back("-mno-zero-initialized-in-bss");
Daniel Dunbar1b718482010-05-14 22:00:22 +0000933
934 // Decide whether to use verbose asm. Verbose assembly is the default on
935 // toolchains which have the integrated assembler on by default.
936 bool IsVerboseAsmDefault = getToolChain().IsIntegratedAssemblerDefault();
937 if (Args.hasFlag(options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
938 IsVerboseAsmDefault) ||
939 Args.hasArg(options::OPT_dA))
Daniel Dunbarf219e7c2009-11-29 07:18:39 +0000940 CmdArgs.push_back("-masm-verbose");
Daniel Dunbar1b718482010-05-14 22:00:22 +0000941
Daniel Dunbarf219e7c2009-11-29 07:18:39 +0000942 if (Args.hasArg(options::OPT_fdebug_pass_structure)) {
943 CmdArgs.push_back("-mdebug-pass");
944 CmdArgs.push_back("Structure");
945 }
946 if (Args.hasArg(options::OPT_fdebug_pass_arguments)) {
947 CmdArgs.push_back("-mdebug-pass");
948 CmdArgs.push_back("Arguments");
949 }
950
John McCalld0c2ec42010-02-19 02:45:38 +0000951 // Enable -mconstructor-aliases except on darwin, where we have to
952 // work around a linker bug; see <rdar://problem/7651567>.
953 if (getToolChain().getTriple().getOS() != llvm::Triple::Darwin)
954 CmdArgs.push_back("-mconstructor-aliases");
955
Daniel Dunbar6bea73b2009-09-16 06:17:29 +0000956 // This is a coarse approximation of what llvm-gcc actually does, both
957 // -fasynchronous-unwind-tables and -fnon-call-exceptions interact in more
958 // complicated ways.
959 bool AsynchronousUnwindTables =
960 Args.hasFlag(options::OPT_fasynchronous_unwind_tables,
961 options::OPT_fno_asynchronous_unwind_tables,
962 getToolChain().IsUnwindTablesDefault() &&
Daniel Dunbar0a80ba72010-03-20 04:52:14 +0000963 !KernelOrKext);
Daniel Dunbar6bea73b2009-09-16 06:17:29 +0000964 if (Args.hasFlag(options::OPT_funwind_tables, options::OPT_fno_unwind_tables,
965 AsynchronousUnwindTables))
Daniel Dunbarf219e7c2009-11-29 07:18:39 +0000966 CmdArgs.push_back("-munwind-tables");
967
968 if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
969 CmdArgs.push_back("-mlimit-float-precision");
970 CmdArgs.push_back(A->getValue(Args));
971 }
Daniel Dunbarbc85be82009-04-29 18:32:25 +0000972
Daniel Dunbar868bd0a2009-05-06 03:16:41 +0000973 // FIXME: Handle -mtune=.
974 (void) Args.hasArg(options::OPT_mtune_EQ);
Daniel Dunbarbc85be82009-04-29 18:32:25 +0000975
Benjamin Kramer8e9ef0d2009-08-05 14:30:52 +0000976 if (Arg *A = Args.getLastArg(options::OPT_mcmodel_EQ)) {
Daniel Dunbarf219e7c2009-11-29 07:18:39 +0000977 CmdArgs.push_back("-mcode-model");
Benjamin Kramer8e9ef0d2009-08-05 14:30:52 +0000978 CmdArgs.push_back(A->getValue(Args));
979 }
980
Daniel Dunbar6acda162009-09-09 22:33:08 +0000981 // Add target specific cpu and features flags.
982 switch(getToolChain().getTriple().getArch()) {
983 default:
984 break;
Daniel Dunbar868bd0a2009-05-06 03:16:41 +0000985
Daniel Dunbarb163ef72009-09-10 04:57:17 +0000986 case llvm::Triple::arm:
987 case llvm::Triple::thumb:
988 AddARMTargetArgs(Args, CmdArgs);
989 break;
990
Eric Christophered734732010-03-02 02:41:08 +0000991 case llvm::Triple::mips:
992 case llvm::Triple::mipsel:
993 AddMIPSTargetArgs(Args, CmdArgs);
994 break;
995
Daniel Dunbar6acda162009-09-09 22:33:08 +0000996 case llvm::Triple::x86:
997 case llvm::Triple::x86_64:
998 AddX86TargetArgs(Args, CmdArgs);
999 break;
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001000 }
1001
Dan Gohmanc31176d2010-01-08 02:20:44 +00001002 // -fno-math-errno is default.
1003 if (Args.hasFlag(options::OPT_fmath_errno,
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001004 options::OPT_fno_math_errno,
Dan Gohmanc31176d2010-01-08 02:20:44 +00001005 false))
1006 CmdArgs.push_back("-fmath-errno");
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001007
Daniel Dunbarb30575c2010-05-12 18:19:58 +00001008 // Explicitly error on some things we know we don't support and can't just
1009 // ignore.
1010 types::ID InputType = Inputs[0].getType();
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001011 Arg *Unsupported;
1012 if ((Unsupported = Args.getLastArg(options::OPT_MG)) ||
Ted Kremenek6feaf572009-12-12 00:38:18 +00001013 (Unsupported = Args.getLastArg(options::OPT_iframework)) ||
1014 (Unsupported = Args.getLastArg(options::OPT_fshort_enums)))
Daniel Dunbare027a4b2009-05-22 19:02:20 +00001015 D.Diag(clang::diag::err_drv_clang_unsupported)
Daniel Dunbarbc85be82009-04-29 18:32:25 +00001016 << Unsupported->getOption().getName();
Daniel Dunbar1d460332009-03-18 10:01:51 +00001017
Daniel Dunbarb30575c2010-05-12 18:19:58 +00001018 if (types::isCXX(InputType) &&
1019 getToolChain().getTriple().getOS() == llvm::Triple::Darwin &&
1020 getToolChain().getTriple().getArch() == llvm::Triple::x86) {
1021 if ((Unsupported = Args.getLastArg(options::OPT_fapple_kext)))
1022 D.Diag(clang::diag::err_drv_clang_unsupported_opt_cxx_darwin_i386)
1023 << Unsupported->getOption().getName();
1024 }
1025
Daniel Dunbar1d460332009-03-18 10:01:51 +00001026 Args.AddAllArgs(CmdArgs, options::OPT_v);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001027 Args.AddLastArg(CmdArgs, options::OPT_P);
Mike Stump1eb44332009-09-09 15:08:12 +00001028 Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001029
1030 // Special case debug options to only pass -g to clang. This is
1031 // wrong.
Rafael Espindola18f36d92010-03-07 04:46:18 +00001032 Args.ClaimAllArgs(options::OPT_g_Group);
Daniel Dunbar6b3d5a62010-05-12 18:19:55 +00001033 if (Arg *A = Args.getLastArg(options::OPT_g_Group))
1034 if (!A->getOption().matches(options::OPT_g0))
1035 CmdArgs.push_back("-g");
Daniel Dunbar1d460332009-03-18 10:01:51 +00001036
Rafael Espindola9cf933a2010-05-06 21:06:04 +00001037 Args.AddAllArgs(CmdArgs, options::OPT_ffunction_sections);
1038 Args.AddAllArgs(CmdArgs, options::OPT_fdata_sections);
1039
Chris Lattner7255a2d2010-06-22 00:03:40 +00001040 Args.AddAllArgs(CmdArgs, options::OPT_finstrument_functions);
1041
Daniel Dunbar1d460332009-03-18 10:01:51 +00001042 Args.AddLastArg(CmdArgs, options::OPT_nostdinc);
Douglas Gregor4c2bcad2010-03-24 20:13:48 +00001043 Args.AddLastArg(CmdArgs, options::OPT_nostdincxx);
Rafael Espindola8d737cc2009-10-26 13:36:57 +00001044 Args.AddLastArg(CmdArgs, options::OPT_nobuiltininc);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001045
Daniel Dunbar5f122322009-12-15 01:02:52 +00001046 // Pass the path to compiler resource files.
Daniel Dunbar5f122322009-12-15 01:02:52 +00001047 CmdArgs.push_back("-resource-dir");
Daniel Dunbar225c4172010-01-20 02:35:16 +00001048 CmdArgs.push_back(D.ResourceDir.c_str());
Daniel Dunbar2ac9fc22009-04-07 21:42:00 +00001049
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001050 // Add preprocessing options like -I, -D, etc. if we are using the
1051 // preprocessor.
1052 //
1053 // FIXME: Support -fpreprocessed
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001054 if (types::getPreprocessedType(InputType) != types::TY_INVALID)
Douglas Gregordf91ef32009-04-18 00:34:01 +00001055 AddPreprocessingOptions(D, Args, CmdArgs, Output, Inputs);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001056
Daniel Dunbar20f0eac2009-09-17 06:53:36 +00001057 // Manually translate -O to -O2 and -O4 to -O3; let clang reject
Daniel Dunbar337a6272009-03-24 20:17:30 +00001058 // others.
1059 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
Daniel Dunbarb827a052009-11-19 03:26:40 +00001060 if (A->getOption().matches(options::OPT_O4))
Daniel Dunbar337a6272009-03-24 20:17:30 +00001061 CmdArgs.push_back("-O3");
Daniel Dunbar473916c2010-05-27 06:51:08 +00001062 else if (A->getOption().matches(options::OPT_O) &&
1063 A->getValue(Args)[0] == '\0')
Daniel Dunbar20f0eac2009-09-17 06:53:36 +00001064 CmdArgs.push_back("-O2");
Daniel Dunbar1d460332009-03-18 10:01:51 +00001065 else
Daniel Dunbar5697aa02009-03-18 23:39:35 +00001066 A->render(Args, CmdArgs);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001067 }
1068
Daniel Dunbar6e8371e2009-10-29 02:24:45 +00001069 Args.AddAllArgs(CmdArgs, options::OPT_W_Group);
1070 Args.AddLastArg(CmdArgs, options::OPT_pedantic);
1071 Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001072 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbard573d262009-04-07 22:13:21 +00001073
1074 // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
1075 // (-ansi is equivalent to -std=c89).
1076 //
1077 // If a std is supplied, only add -trigraphs if it follows the
1078 // option.
1079 if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
1080 if (Std->getOption().matches(options::OPT_ansi))
Nuno Lopes528365d2009-10-16 14:28:06 +00001081 if (types::isCXX(InputType))
Daniel Dunbar294691e2009-11-04 06:24:38 +00001082 CmdArgs.push_back("-std=c++98");
Nuno Lopes528365d2009-10-16 14:28:06 +00001083 else
Daniel Dunbar294691e2009-11-04 06:24:38 +00001084 CmdArgs.push_back("-std=c89");
Daniel Dunbard573d262009-04-07 22:13:21 +00001085 else
1086 Std->render(Args, CmdArgs);
1087
Daniel Dunbar0e100312010-06-14 21:23:08 +00001088 if (Arg *A = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi,
1089 options::OPT_trigraphs))
1090 if (A != Std)
Daniel Dunbard573d262009-04-07 22:13:21 +00001091 A->render(Args, CmdArgs);
Daniel Dunbara3ff2022009-04-26 01:10:38 +00001092 } else {
1093 // Honor -std-default.
Daniel Dunbar4a5290e2010-01-29 21:03:02 +00001094 //
1095 // FIXME: Clang doesn't correctly handle -std= when the input language
1096 // doesn't match. For the time being just ignore this for C++ inputs;
1097 // eventually we want to do all the standard defaulting here instead of
1098 // splitting it between the driver and clang -cc1.
1099 if (!types::isCXX(InputType))
1100 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
1101 "-std=", /*Joined=*/true);
Daniel Dunbard573d262009-04-07 22:13:21 +00001102 Args.AddLastArg(CmdArgs, options::OPT_trigraphs);
Daniel Dunbara3ff2022009-04-26 01:10:38 +00001103 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001104
Chandler Carruthc304ba32010-05-22 02:21:53 +00001105 // Translate GCC's misnamer '-fasm' arguments to '-fgnu-keywords'.
1106 if (Arg *Asm = Args.getLastArg(options::OPT_fasm, options::OPT_fno_asm)) {
1107 if (Asm->getOption().matches(options::OPT_fasm))
1108 CmdArgs.push_back("-fgnu-keywords");
1109 else
1110 CmdArgs.push_back("-fno-gnu-keywords");
1111 }
1112
Daniel Dunbar1d460332009-03-18 10:01:51 +00001113 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_)) {
1114 CmdArgs.push_back("-ftemplate-depth");
1115 CmdArgs.push_back(A->getValue(Args));
1116 }
1117
Daniel Dunbare4bdae72009-11-19 04:00:53 +00001118 if (Args.hasArg(options::OPT__relocatable_pch))
Daniel Dunbar66861e02009-11-20 22:21:36 +00001119 CmdArgs.push_back("-relocatable-pch");
Mike Stump1eb44332009-09-09 15:08:12 +00001120
Daniel Dunbar294691e2009-11-04 06:24:38 +00001121 if (Arg *A = Args.getLastArg(options::OPT_fconstant_string_class_EQ)) {
1122 CmdArgs.push_back("-fconstant-string-class");
1123 CmdArgs.push_back(A->getValue(Args));
1124 }
David Chisnall8a5a9aa2009-08-31 16:41:57 +00001125
Chris Lattner124fca52010-01-09 21:54:33 +00001126 if (Arg *A = Args.getLastArg(options::OPT_ftabstop_EQ)) {
1127 CmdArgs.push_back("-ftabstop");
1128 CmdArgs.push_back(A->getValue(Args));
1129 }
1130
Chris Lattner0f0c9632010-04-07 20:49:23 +00001131 CmdArgs.push_back("-ferror-limit");
1132 if (Arg *A = Args.getLastArg(options::OPT_ferror_limit_EQ))
1133 CmdArgs.push_back(A->getValue(Args));
1134 else
1135 CmdArgs.push_back("19");
Douglas Gregor575cf372010-04-20 07:18:24 +00001136
Chandler Carruthc40f73c2010-05-06 04:55:18 +00001137 if (Arg *A = Args.getLastArg(options::OPT_fmacro_backtrace_limit_EQ)) {
1138 CmdArgs.push_back("-fmacro-backtrace-limit");
Douglas Gregor6c1cb992010-05-04 17:13:42 +00001139 CmdArgs.push_back(A->getValue(Args));
Chandler Carruthc40f73c2010-05-06 04:55:18 +00001140 }
1141
1142 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_backtrace_limit_EQ)) {
1143 CmdArgs.push_back("-ftemplate-backtrace-limit");
Douglas Gregor575cf372010-04-20 07:18:24 +00001144 CmdArgs.push_back(A->getValue(Args));
Chandler Carruthc40f73c2010-05-06 04:55:18 +00001145 }
1146
Daniel Dunbar55efe142009-11-04 06:24:47 +00001147 // Pass -fmessage-length=.
Daniel Dunbara28690e2009-11-30 08:40:54 +00001148 CmdArgs.push_back("-fmessage-length");
Daniel Dunbar55efe142009-11-04 06:24:47 +00001149 if (Arg *A = Args.getLastArg(options::OPT_fmessage_length_EQ)) {
Daniel Dunbara28690e2009-11-30 08:40:54 +00001150 CmdArgs.push_back(A->getValue(Args));
Daniel Dunbar55efe142009-11-04 06:24:47 +00001151 } else {
1152 // If -fmessage-length=N was not specified, determine whether this is a
1153 // terminal and, if so, implicitly define -fmessage-length appropriately.
1154 unsigned N = llvm::sys::Process::StandardErrColumns();
Daniel Dunbar55efe142009-11-04 06:24:47 +00001155 CmdArgs.push_back(Args.MakeArgString(llvm::Twine(N)));
1156 }
1157
Daniel Dunbarba8d8612009-12-03 18:42:11 +00001158 if (const Arg *A = Args.getLastArg(options::OPT_fvisibility_EQ)) {
1159 CmdArgs.push_back("-fvisibility");
1160 CmdArgs.push_back(A->getValue(Args));
1161 }
1162
Douglas Gregor7cf84d62010-06-15 17:05:35 +00001163 Args.AddLastArg(CmdArgs, options::OPT_fvisibility_inlines_hidden);
1164
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00001165 // -fhosted is default.
1166 if (KernelOrKext || Args.hasFlag(options::OPT_ffreestanding,
1167 options::OPT_fhosted,
1168 false))
1169 CmdArgs.push_back("-ffreestanding");
1170
Daniel Dunbarba8d8612009-12-03 18:42:11 +00001171 // Forward -f (flag) options which we can pass directly.
Mike Stump9c276ae2009-12-12 01:27:46 +00001172 Args.AddLastArg(CmdArgs, options::OPT_fcatch_undefined_behavior);
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00001173 Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00001174 Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
Daniel Dunbar8c6fa842010-03-16 16:57:46 +00001175
1176 // -flax-vector-conversions is default.
1177 if (!Args.hasFlag(options::OPT_flax_vector_conversions,
1178 options::OPT_fno_lax_vector_conversions))
1179 CmdArgs.push_back("-fno-lax-vector-conversions");
1180
Daniel Dunbar43a9b322010-04-10 16:20:23 +00001181 // Handle -fobjc-gc and -fobjc-gc-only. They are exclusive, and -fobjc-gc-only
1182 // takes precedence.
1183 const Arg *GCArg = Args.getLastArg(options::OPT_fobjc_gc_only);
1184 if (!GCArg)
1185 GCArg = Args.getLastArg(options::OPT_fobjc_gc);
1186 if (GCArg) {
1187 if (getToolChain().SupportsObjCGC()) {
1188 GCArg->render(Args, CmdArgs);
1189 } else {
1190 // FIXME: We should move this to a hard error.
1191 D.Diag(clang::diag::warn_drv_objc_gc_unsupported)
1192 << GCArg->getAsString(Args);
1193 }
1194 }
1195
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00001196 Args.AddLastArg(CmdArgs, options::OPT_fno_show_column);
Fariborz Jahanian34e65772009-05-22 20:17:16 +00001197 Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
Chris Lattner182e0922009-04-21 05:34:31 +00001198 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00001199 Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
1200 Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
Chris Lattnera4d71452010-06-26 21:25:03 +00001201 Args.AddLastArg(CmdArgs, options::OPT_fwrapv);
Daniel Dunbar3aaf0822009-04-07 21:51:40 +00001202 Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001203
Daniel Dunbar5345c392009-09-03 04:54:28 +00001204 Args.AddLastArg(CmdArgs, options::OPT_pthread);
1205
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001206 // -stack-protector=0 is default.
1207 unsigned StackProtectorLevel = 0;
Bill Wendling45483f72009-06-28 07:36:13 +00001208 if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
1209 options::OPT_fstack_protector_all,
1210 options::OPT_fstack_protector)) {
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001211 if (A->getOption().matches(options::OPT_fstack_protector))
1212 StackProtectorLevel = 1;
1213 else if (A->getOption().matches(options::OPT_fstack_protector_all))
1214 StackProtectorLevel = 2;
1215 } else
1216 StackProtectorLevel = getToolChain().GetDefaultStackProtectorLevel();
1217 if (StackProtectorLevel) {
1218 CmdArgs.push_back("-stack-protector");
1219 CmdArgs.push_back(Args.MakeArgString(llvm::Twine(StackProtectorLevel)));
Bill Wendling45483f72009-06-28 07:36:13 +00001220 }
1221
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00001222 // Forward -f options with positive and negative forms; we translate
1223 // these by hand.
1224
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001225 // -fbuiltin is default.
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00001226 if (!Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin))
Daniel Dunbar53e84842009-11-19 04:55:23 +00001227 CmdArgs.push_back("-fno-builtin");
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00001228
Nuno Lopesfc284482009-12-16 16:59:22 +00001229 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
1230 options::OPT_fno_assume_sane_operator_new))
1231 CmdArgs.push_back("-fno-assume-sane-operator-new");
1232
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001233 // -fblocks=0 is default.
1234 if (Args.hasFlag(options::OPT_fblocks, options::OPT_fno_blocks,
David Chisnall5e530af2009-11-17 19:33:30 +00001235 getToolChain().IsBlocksDefault())) {
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001236 CmdArgs.push_back("-fblocks");
David Chisnall5e530af2009-11-17 19:33:30 +00001237 }
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00001238
John McCall32579cf2010-04-09 19:12:06 +00001239 // -faccess-control is default.
John McCall7002f4c2010-04-09 19:03:51 +00001240 if (Args.hasFlag(options::OPT_fno_access_control,
1241 options::OPT_faccess_control,
John McCall32579cf2010-04-09 19:12:06 +00001242 false))
John McCall7002f4c2010-04-09 19:03:51 +00001243 CmdArgs.push_back("-fno-access-control");
John McCall3ddd6e02010-03-17 01:32:13 +00001244
Daniel Dunbar6d2eb4d2009-11-25 10:14:30 +00001245 // -fexceptions=0 is default.
Fariborz Jahanian85caf032009-10-01 20:30:46 +00001246 if (needsExceptions(Args, InputType, getToolChain().getTriple()))
Rafael Espindolaf759df02009-10-01 13:33:33 +00001247 CmdArgs.push_back("-fexceptions");
Mike Stump738f8c22009-07-31 23:15:31 +00001248
Daniel Dunbarb2987d12010-02-10 18:49:11 +00001249 if (getToolChain().UseSjLjExceptions())
1250 CmdArgs.push_back("-fsjlj-exceptions");
1251
Daniel Dunbar0be42c42009-11-17 07:06:20 +00001252 // -frtti is default.
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00001253 if (KernelOrKext ||
1254 !Args.hasFlag(options::OPT_frtti, options::OPT_fno_rtti))
Daniel Dunbar53e84842009-11-19 04:55:23 +00001255 CmdArgs.push_back("-fno-rtti");
Mike Stump738f8c22009-07-31 23:15:31 +00001256
Daniel Dunbar1f95e652009-11-17 06:37:03 +00001257 // -fsigned-char is default.
Daniel Dunbar6d2eb4d2009-11-25 10:14:30 +00001258 if (!Args.hasFlag(options::OPT_fsigned_char, options::OPT_funsigned_char,
Daniel Dunbar1f95e652009-11-17 06:37:03 +00001259 isSignedCharDefault(getToolChain().getTriple())))
Daniel Dunbar76743522009-11-29 02:39:08 +00001260 CmdArgs.push_back("-fno-signed-char");
Eli Friedman5a779732009-06-05 07:21:14 +00001261
Anders Carlssona508b7d2010-02-06 23:23:06 +00001262 // -fthreadsafe-static is default.
1263 if (!Args.hasFlag(options::OPT_fthreadsafe_statics,
1264 options::OPT_fno_threadsafe_statics))
1265 CmdArgs.push_back("-fno-threadsafe-statics");
1266
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00001267 // -fuse-cxa-atexit is default.
Daniel Dunbar0a80ba72010-03-20 04:52:14 +00001268 if (KernelOrKext || !Args.hasFlag(options::OPT_fuse_cxa_atexit,
1269 options::OPT_fno_use_cxa_atexit))
Daniel Dunbarefb0fa92010-03-20 04:15:41 +00001270 CmdArgs.push_back("-fno-use-cxa-atexit");
1271
Daniel Dunbar0be42c42009-11-17 07:06:20 +00001272 // -fms-extensions=0 is default.
Daniel Dunbar6d2eb4d2009-11-25 10:14:30 +00001273 if (Args.hasFlag(options::OPT_fms_extensions, options::OPT_fno_ms_extensions,
Daniel Dunbar0be42c42009-11-17 07:06:20 +00001274 getToolChain().getTriple().getOS() == llvm::Triple::Win32))
1275 CmdArgs.push_back("-fms-extensions");
1276
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00001277 // -fgnu-keywords default varies depending on language; only pass if
1278 // specified.
1279 if (Arg *A = Args.getLastArg(options::OPT_fgnu_keywords,
Daniel Dunbar40788d92010-04-24 17:56:39 +00001280 options::OPT_fno_gnu_keywords))
1281 A->render(Args, CmdArgs);
Chandler Carrutheb5d7b72010-04-17 20:17:31 +00001282
Daniel Dunbarc5a97ec2009-11-17 07:07:28 +00001283 // -fnext-runtime is default.
Daniel Dunbar6d2eb4d2009-11-25 10:14:30 +00001284 if (!Args.hasFlag(options::OPT_fnext_runtime, options::OPT_fgnu_runtime,
Daniel Dunbarc5a97ec2009-11-17 07:07:28 +00001285 getToolChain().getTriple().getOS() == llvm::Triple::Darwin))
1286 CmdArgs.push_back("-fgnu-runtime");
1287
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001288 // -fobjc-nonfragile-abi=0 is default.
1289 if (types::isObjC(InputType)) {
Daniel Dunbar5e2a8ac2010-04-28 23:25:24 +00001290 unsigned Version = 1;
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001291 if (Args.hasArg(options::OPT_fobjc_nonfragile_abi) ||
Daniel Dunbar5e2a8ac2010-04-28 23:25:24 +00001292 getToolChain().IsObjCNonFragileABIDefault())
1293 Version = 2;
1294 if (Arg *A = Args.getLastArg(options::OPT_fobjc_abi_version_EQ)) {
1295 if (llvm::StringRef(A->getValue(Args)) == "1")
1296 Version = 1;
1297 else if (llvm::StringRef(A->getValue(Args)) == "2")
1298 Version = 2;
1299 else
1300 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
1301 }
1302
1303 if (Version == 2) {
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001304 CmdArgs.push_back("-fobjc-nonfragile-abi");
Daniel Dunbarf643b9b2010-04-24 17:56:46 +00001305
1306 // -fobjc-dispatch-method is only relevant with the nonfragile-abi, and
1307 // legacy is the default.
1308 if (!Args.hasFlag(options::OPT_fobjc_legacy_dispatch,
1309 options::OPT_fno_objc_legacy_dispatch,
1310 getToolChain().IsObjCLegacyDispatchDefault())) {
1311 if (getToolChain().UseObjCMixedDispatch())
1312 CmdArgs.push_back("-fobjc-dispatch-method=mixed");
1313 else
1314 CmdArgs.push_back("-fobjc-dispatch-method=non-legacy");
1315 }
Daniel Dunbar984eb862010-02-01 21:07:25 +00001316 }
Ted Kremenekc3e31d62010-05-20 02:12:37 +00001317
1318 // FIXME: -fobjc-nonfragile-abi2 is a transient option meant to expose
1319 // features in testing. It will eventually be removed.
1320 if (Args.hasArg(options::OPT_fobjc_nonfragile_abi2))
1321 CmdArgs.push_back("-fobjc-nonfragile-abi2");
Daniel Dunbar9e5cc6b2009-11-17 08:07:36 +00001322 }
1323
Daniel Dunbar984eb862010-02-01 21:07:25 +00001324 if (!Args.hasFlag(options::OPT_fassume_sane_operator_new,
1325 options::OPT_fno_assume_sane_operator_new))
1326 CmdArgs.push_back("-fno-assume-sane-operator-new");
1327
Daniel Dunbarf35f14d2010-04-27 15:34:57 +00001328 // -fconstant-cfstrings is default, and may be subject to argument translation
1329 // on Darwin.
1330 if (!Args.hasFlag(options::OPT_fconstant_cfstrings,
1331 options::OPT_fno_constant_cfstrings) ||
1332 !Args.hasFlag(options::OPT_mconstant_cfstrings,
1333 options::OPT_mno_constant_cfstrings))
1334 CmdArgs.push_back("-fno-constant-cfstrings");
1335
John Thompsona6fda122009-11-05 20:14:16 +00001336 // -fshort-wchar default varies depending on platform; only
1337 // pass if specified.
Daniel Dunbar1744a352010-04-27 15:35:03 +00001338 if (Arg *A = Args.getLastArg(options::OPT_fshort_wchar))
1339 A->render(Args, CmdArgs);
John Thompsona6fda122009-11-05 20:14:16 +00001340
Daniel Dunbaree848a72009-10-29 02:39:57 +00001341 // -fno-pascal-strings is default, only pass non-default. If the tool chain
1342 // happened to translate to -mpascal-strings, we want to back translate here.
Daniel Dunbar82d00682009-04-07 23:51:44 +00001343 //
1344 // FIXME: This is gross; that translation should be pulled from the
1345 // tool chain.
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001346 if (Args.hasFlag(options::OPT_fpascal_strings,
Daniel Dunbar82d00682009-04-07 23:51:44 +00001347 options::OPT_fno_pascal_strings,
1348 false) ||
1349 Args.hasFlag(options::OPT_mpascal_strings,
1350 options::OPT_mno_pascal_strings,
1351 false))
Daniel Dunbar48d1ef72009-04-07 21:16:11 +00001352 CmdArgs.push_back("-fpascal-strings");
1353
1354 // -fcommon is default, only pass non-default.
1355 if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common))
1356 CmdArgs.push_back("-fno-common");
1357
Daniel Dunbar70d3c922009-04-15 02:37:43 +00001358 // -fsigned-bitfields is default, and clang doesn't yet support
1359 // --funsigned-bitfields.
Mike Stump1eb44332009-09-09 15:08:12 +00001360 if (!Args.hasFlag(options::OPT_fsigned_bitfields,
Daniel Dunbar70d3c922009-04-15 02:37:43 +00001361 options::OPT_funsigned_bitfields))
1362 D.Diag(clang::diag::warn_drv_clang_unsupported)
1363 << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
1364
Jeffrey Yasskin0ea22fd2010-06-08 04:56:20 +00001365 // -fcaret-diagnostics is default.
1366 if (!Args.hasFlag(options::OPT_fcaret_diagnostics,
1367 options::OPT_fno_caret_diagnostics, true))
1368 CmdArgs.push_back("-fno-caret-diagnostics");
1369
Daniel Dunbar49138fc2009-04-19 21:09:34 +00001370 // -fdiagnostics-fixit-info is default, only pass non-default.
Mike Stump1eb44332009-09-09 15:08:12 +00001371 if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
Daniel Dunbar49138fc2009-04-19 21:09:34 +00001372 options::OPT_fno_diagnostics_fixit_info))
1373 CmdArgs.push_back("-fno-diagnostics-fixit-info");
1374
Daniel Dunbar74f20832010-01-30 23:31:57 +00001375 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_binary);
Douglas Gregord93256e2010-01-28 06:00:51 +00001376
Daniel Dunbar9e820ee2009-04-16 06:32:38 +00001377 // Enable -fdiagnostics-show-option by default.
Mike Stump1eb44332009-09-09 15:08:12 +00001378 if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
Daniel Dunbar9e820ee2009-04-16 06:32:38 +00001379 options::OPT_fno_diagnostics_show_option))
1380 CmdArgs.push_back("-fdiagnostics-show-option");
Daniel Dunbar838be482009-11-04 06:24:57 +00001381
Chris Lattner6fbe8392010-05-04 21:55:25 +00001382 if (const Arg *A =
1383 Args.getLastArg(options::OPT_fdiagnostics_show_category_EQ)) {
1384 CmdArgs.push_back("-fdiagnostics-show-category");
1385 CmdArgs.push_back(A->getValue(Args));
1386 }
1387
Daniel Dunbar838be482009-11-04 06:24:57 +00001388 // Color diagnostics are the default, unless the terminal doesn't support
1389 // them.
1390 if (Args.hasFlag(options::OPT_fcolor_diagnostics,
1391 options::OPT_fno_color_diagnostics) &&
1392 llvm::sys::Process::StandardErrHasColors())
1393 CmdArgs.push_back("-fcolor-diagnostics");
1394
Daniel Dunbar75eb1d62009-06-08 21:13:54 +00001395 if (!Args.hasFlag(options::OPT_fshow_source_location,
1396 options::OPT_fno_show_source_location))
1397 CmdArgs.push_back("-fno-show-source-location");
Daniel Dunbar9e820ee2009-04-16 06:32:38 +00001398
Jeffrey Yasskin5edbdcc2010-06-11 05:57:47 +00001399 if (Arg *A = Args.getLastArg(options::OPT_fshow_overloads_EQ))
1400 A->render(Args, CmdArgs);
1401
Daniel Dunbar7695fba2009-04-19 21:20:32 +00001402 // -fdollars-in-identifiers default varies depending on platform and
1403 // language; only pass if specified.
Mike Stump1eb44332009-09-09 15:08:12 +00001404 if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
Daniel Dunbar7695fba2009-04-19 21:20:32 +00001405 options::OPT_fno_dollars_in_identifiers)) {
1406 if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
Daniel Dunbar8663b182009-12-16 20:10:18 +00001407 CmdArgs.push_back("-fdollars-in-identifiers");
Daniel Dunbar7695fba2009-04-19 21:20:32 +00001408 else
Daniel Dunbar8663b182009-12-16 20:10:18 +00001409 CmdArgs.push_back("-fno-dollars-in-identifiers");
Daniel Dunbar7695fba2009-04-19 21:20:32 +00001410 }
1411
Daniel Dunbare027a4b2009-05-22 19:02:20 +00001412 // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
1413 // practical purposes.
Mike Stump1eb44332009-09-09 15:08:12 +00001414 if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
Daniel Dunbare027a4b2009-05-22 19:02:20 +00001415 options::OPT_fno_unit_at_a_time)) {
1416 if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
Edward O'Callaghanc3769102009-10-13 16:41:34 +00001417 D.Diag(clang::diag::warn_drv_clang_unsupported) << A->getAsString(Args);
Daniel Dunbare027a4b2009-05-22 19:02:20 +00001418 }
Eli Friedmanceb5c5b2009-07-14 21:58:17 +00001419
Daniel Dunbar2ba91572009-09-10 03:37:02 +00001420 // Default to -fno-builtin-str{cat,cpy} on Darwin for ARM.
Daniel Dunbarf84a4a42009-09-10 04:57:27 +00001421 //
Daniel Dunbar8ff5b282009-12-11 23:00:49 +00001422 // FIXME: This is disabled until clang -cc1 supports -fno-builtin-foo. PR4941.
Daniel Dunbarf84a4a42009-09-10 04:57:27 +00001423#if 0
Daniel Dunbar2ba91572009-09-10 03:37:02 +00001424 if (getToolChain().getTriple().getOS() == llvm::Triple::Darwin &&
1425 (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
1426 getToolChain().getTriple().getArch() == llvm::Triple::thumb)) {
1427 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
1428 CmdArgs.push_back("-fno-builtin-strcat");
1429 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
1430 CmdArgs.push_back("-fno-builtin-strcpy");
1431 }
Daniel Dunbarf84a4a42009-09-10 04:57:27 +00001432#endif
Daniel Dunbar2ba91572009-09-10 03:37:02 +00001433
Mike Stump1eb44332009-09-09 15:08:12 +00001434 if (Arg *A = Args.getLastArg(options::OPT_traditional,
Eli Friedmanceb5c5b2009-07-14 21:58:17 +00001435 options::OPT_traditional_cpp))
1436 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
1437
Daniel Dunbar1d460332009-03-18 10:01:51 +00001438 Args.AddLastArg(CmdArgs, options::OPT_dM);
Chris Lattnerd82df3a2009-04-12 01:56:53 +00001439 Args.AddLastArg(CmdArgs, options::OPT_dD);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001440
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00001441 // Forward -Xclang arguments to -cc1, and -mllvm arguments to the LLVM option
1442 // parser.
Daniel Dunbar1d460332009-03-18 10:01:51 +00001443 Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00001444 for (arg_iterator it = Args.filtered_begin(options::OPT_mllvm),
1445 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00001446 (*it)->claim();
Daniel Dunbarfb36d212010-04-17 06:10:00 +00001447
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00001448 // We translate this by hand to the -cc1 argument, since nightly test uses
1449 // it and developers have been trained to spell it with -mllvm.
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00001450 if (llvm::StringRef((*it)->getValue(Args, 0)) == "-disable-llvm-optzns")
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00001451 CmdArgs.push_back("-disable-llvm-optzns");
1452 else
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00001453 (*it)->render(Args, CmdArgs);
Daniel Dunbar3f87fb02010-04-15 06:09:03 +00001454 }
Daniel Dunbar1d460332009-03-18 10:01:51 +00001455
Daniel Dunbarcd8e4c42009-03-30 06:36:42 +00001456 if (Output.getType() == types::TY_Dependencies) {
1457 // Handled with other dependency code.
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00001458 } else if (Output.isPipe()) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001459 CmdArgs.push_back("-o");
1460 CmdArgs.push_back("-");
Daniel Dunbar115a7922009-03-19 07:29:38 +00001461 } else if (Output.isFilename()) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001462 CmdArgs.push_back("-o");
Daniel Dunbar115a7922009-03-19 07:29:38 +00001463 CmdArgs.push_back(Output.getFilename());
1464 } else {
1465 assert(Output.isNothing() && "Invalid output.");
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001466 }
1467
Daniel Dunbar1d460332009-03-18 10:01:51 +00001468 for (InputInfoList::const_iterator
1469 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
1470 const InputInfo &II = *it;
1471 CmdArgs.push_back("-x");
1472 CmdArgs.push_back(types::getTypeName(II.getType()));
1473 if (II.isPipe())
1474 CmdArgs.push_back("-");
Daniel Dunbar115a7922009-03-19 07:29:38 +00001475 else if (II.isFilename())
1476 CmdArgs.push_back(II.getFilename());
Daniel Dunbar1d460332009-03-18 10:01:51 +00001477 else
Daniel Dunbar115a7922009-03-19 07:29:38 +00001478 II.getInputArg().renderAsInput(Args, CmdArgs);
Daniel Dunbar1d460332009-03-18 10:01:51 +00001479 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001480
Chris Lattnere6113de2009-11-03 19:50:27 +00001481 Args.AddAllArgs(CmdArgs, options::OPT_undef);
1482
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001483 const char *Exec =
Daniel Dunbar8ff5b282009-12-11 23:00:49 +00001484 Args.MakeArgString(getToolChain().GetProgramPath(C, "clang"));
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00001485
1486 // Optionally embed the -cc1 level arguments into the debug info, for build
1487 // analysis.
1488 if (getToolChain().UseDwarfDebugFlags()) {
Daniel Dunbar6e900472010-06-04 18:47:06 +00001489 ArgStringList OriginalArgs;
1490 for (ArgList::const_iterator it = Args.begin(),
1491 ie = Args.end(); it != ie; ++it)
1492 (*it)->render(Args, OriginalArgs);
1493
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00001494 llvm::SmallString<256> Flags;
1495 Flags += Exec;
Daniel Dunbar6e900472010-06-04 18:47:06 +00001496 for (unsigned i = 0, e = OriginalArgs.size(); i != e; ++i) {
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00001497 Flags += " ";
Daniel Dunbar6e900472010-06-04 18:47:06 +00001498 Flags += OriginalArgs[i];
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +00001499 }
1500 CmdArgs.push_back("-dwarf-debug-flags");
1501 CmdArgs.push_back(Args.MakeArgString(Flags.str()));
1502 }
1503
Daniel Dunbardaab7b12009-12-02 03:23:25 +00001504 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbara880db02009-03-23 19:03:36 +00001505
Daniel Dunbar5c1aaaf2009-04-07 19:18:24 +00001506 // Explicitly warn that these options are unsupported, even though
1507 // we are allowing compilation to continue.
Daniel Dunbarcdd96862009-11-25 11:53:23 +00001508 for (arg_iterator it = Args.filtered_begin(options::OPT_pg),
1509 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00001510 (*it)->claim();
1511 D.Diag(clang::diag::warn_drv_clang_unsupported) << (*it)->getAsString(Args);
Daniel Dunbar5c1aaaf2009-04-07 19:18:24 +00001512 }
1513
Daniel Dunbar68fb4692009-04-03 20:51:31 +00001514 // Claim some arguments which clang supports automatically.
1515
Daniel Dunbarf4046862010-04-15 06:18:42 +00001516 // -fpch-preprocess is used with gcc to add a special marker in the output to
1517 // include the PCH file. Clang's PTH solution is completely transparent, so we
1518 // do not need to deal with it at all.
Daniel Dunbar68fb4692009-04-03 20:51:31 +00001519 Args.ClaimAllArgs(options::OPT_fpch_preprocess);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001520
Daniel Dunbara880db02009-03-23 19:03:36 +00001521 // Claim some arguments which clang doesn't support, but we don't
1522 // care to warn the user about.
Daniel Dunbarcdd96862009-11-25 11:53:23 +00001523 Args.ClaimAllArgs(options::OPT_clang_ignored_f_Group);
1524 Args.ClaimAllArgs(options::OPT_clang_ignored_m_Group);
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001525}
1526
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00001527void ClangAs::ConstructJob(Compilation &C, const JobAction &JA,
1528 Job &Dest,
1529 const InputInfo &Output,
1530 const InputInfoList &Inputs,
1531 const ArgList &Args,
1532 const char *LinkingOutput) const {
1533 const Driver &D = getToolChain().getDriver();
1534 ArgStringList CmdArgs;
1535
1536 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
1537 const InputInfo &Input = Inputs[0];
1538
1539 // Invoke ourselves in -cc1as mode.
1540 //
1541 // FIXME: Implement custom jobs for internal actions.
1542 CmdArgs.push_back("-cc1as");
1543
1544 // Add the "effective" target triple.
1545 CmdArgs.push_back("-triple");
1546 std::string TripleStr = getEffectiveClangTriple(D, getToolChain(), Args);
1547 CmdArgs.push_back(Args.MakeArgString(TripleStr));
1548
1549 // Set the output mode, we currently only expect to be used as a real
1550 // assembler.
1551 CmdArgs.push_back("-filetype");
1552 CmdArgs.push_back("obj");
1553
Daniel Dunbar99298002010-05-27 06:18:05 +00001554 // At -O0, we use -mrelax-all by default.
1555 bool IsOpt = false;
1556 if (Arg *A = Args.getLastArg(options::OPT_O_Group))
1557 IsOpt = !A->getOption().matches(options::OPT_O0);
1558 if (Args.hasFlag(options::OPT_mrelax_all,
1559 options::OPT_mno_relax_all,
1560 !IsOpt))
Daniel Dunbar469d40e2010-05-28 16:43:21 +00001561 CmdArgs.push_back("-relax-all");
Daniel Dunbar99298002010-05-27 06:18:05 +00001562
Daniel Dunbar20a9aa52010-05-20 21:30:13 +00001563 // FIXME: Add -force_cpusubtype_ALL support, once we have it.
1564
1565 // FIXME: Add -g support, once we have it.
1566
1567 // FIXME: Add -static support, once we have it.
1568
1569 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
1570 options::OPT_Xassembler);
1571
1572 assert(Output.isFilename() && "Unexpected lipo output.");
1573 CmdArgs.push_back("-o");
1574 CmdArgs.push_back(Output.getFilename());
1575
1576 if (Input.isPipe()) {
1577 CmdArgs.push_back("-");
1578 } else {
1579 assert(Input.isFilename() && "Invalid input.");
1580 CmdArgs.push_back(Input.getFilename());
1581 }
1582
1583 const char *Exec =
1584 Args.MakeArgString(getToolChain().GetProgramPath(C, "clang"));
1585 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
1586}
1587
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001588void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA,
1589 Job &Dest,
1590 const InputInfo &Output,
1591 const InputInfoList &Inputs,
Daniel Dunbar1d460332009-03-18 10:01:51 +00001592 const ArgList &Args,
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001593 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00001594 const Driver &D = getToolChain().getDriver();
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001595 ArgStringList CmdArgs;
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001596
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001597 for (ArgList::const_iterator
Daniel Dunbar1d460332009-03-18 10:01:51 +00001598 it = Args.begin(), ie = Args.end(); it != ie; ++it) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001599 Arg *A = *it;
Daniel Dunbar75877192009-03-19 07:55:12 +00001600 if (A->getOption().hasForwardToGCC()) {
1601 // It is unfortunate that we have to claim here, as this means
1602 // we will basically never report anything interesting for
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00001603 // platforms using a generic gcc, even if we are just using gcc
1604 // to get to the assembler.
Daniel Dunbar75877192009-03-19 07:55:12 +00001605 A->claim();
Daniel Dunbar1d460332009-03-18 10:01:51 +00001606 A->render(Args, CmdArgs);
Daniel Dunbar75877192009-03-19 07:55:12 +00001607 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001608 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001609
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00001610 RenderExtraToolArgs(JA, CmdArgs);
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001611
1612 // If using a driver driver, force the arch.
Daniel Dunbar7cfe31a2009-05-22 02:21:04 +00001613 const std::string &Arch = getToolChain().getArchName();
Daniel Dunbar1bda4342009-12-23 00:46:38 +00001614 if (getToolChain().getTriple().getOS() == llvm::Triple::Darwin) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001615 CmdArgs.push_back("-arch");
Daniel Dunbarbf54a062009-04-01 20:33:11 +00001616
1617 // FIXME: Remove these special cases.
Daniel Dunbar7cfe31a2009-05-22 02:21:04 +00001618 if (Arch == "powerpc")
1619 CmdArgs.push_back("ppc");
1620 else if (Arch == "powerpc64")
1621 CmdArgs.push_back("ppc64");
1622 else
Daniel Dunbar88137642009-09-09 22:32:48 +00001623 CmdArgs.push_back(Args.MakeArgString(Arch));
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001624 }
1625
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00001626 // Try to force gcc to match the tool chain we want, if we recognize
1627 // the arch.
Daniel Dunbar7cfe31a2009-05-22 02:21:04 +00001628 //
1629 // FIXME: The triple class should directly provide the information we want
1630 // here.
1631 if (Arch == "i386" || Arch == "powerpc")
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00001632 CmdArgs.push_back("-m32");
Daniel Dunbar7cfe31a2009-05-22 02:21:04 +00001633 else if (Arch == "x86_64" || Arch == "powerpc64")
Daniel Dunbar6ecc7a92009-05-02 21:41:52 +00001634 CmdArgs.push_back("-m64");
1635
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001636 if (Output.isPipe()) {
1637 CmdArgs.push_back("-o");
1638 CmdArgs.push_back("-");
Daniel Dunbar115a7922009-03-19 07:29:38 +00001639 } else if (Output.isFilename()) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001640 CmdArgs.push_back("-o");
Daniel Dunbar115a7922009-03-19 07:29:38 +00001641 CmdArgs.push_back(Output.getFilename());
1642 } else {
1643 assert(Output.isNothing() && "Unexpected output");
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001644 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar115a7922009-03-19 07:29:38 +00001645 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001646
1647
1648 // Only pass -x if gcc will understand it; otherwise hope gcc
1649 // understands the suffix correctly. The main use case this would go
1650 // wrong in is for linker inputs if they happened to have an odd
1651 // suffix; really the only way to get this to happen is a command
1652 // like '-x foobar a.c' which will treat a.c like a linker input.
1653 //
1654 // FIXME: For the linker case specifically, can we safely convert
1655 // inputs into '-Wl,' options?
1656 for (InputInfoList::const_iterator
1657 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
1658 const InputInfo &II = *it;
Daniel Dunbara8304f62009-05-02 20:14:53 +00001659
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00001660 // Don't try to pass LLVM or AST inputs to a generic gcc.
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00001661 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
1662 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
Daniel Dunbara8304f62009-05-02 20:14:53 +00001663 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00001664 << getToolChain().getTripleString();
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00001665 else if (II.getType() == types::TY_AST)
1666 D.Diag(clang::diag::err_drv_no_ast_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00001667 << getToolChain().getTripleString();
Daniel Dunbara8304f62009-05-02 20:14:53 +00001668
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001669 if (types::canTypeBeUserSpecified(II.getType())) {
1670 CmdArgs.push_back("-x");
1671 CmdArgs.push_back(types::getTypeName(II.getType()));
1672 }
1673
1674 if (II.isPipe())
1675 CmdArgs.push_back("-");
Daniel Dunbar115a7922009-03-19 07:29:38 +00001676 else if (II.isFilename())
1677 CmdArgs.push_back(II.getFilename());
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001678 else
Daniel Dunbar115a7922009-03-19 07:29:38 +00001679 // Don't render as input, we need gcc to do the translations.
1680 II.getInputArg().render(Args, CmdArgs);
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001681 }
1682
Daniel Dunbaree788e72009-12-21 18:54:17 +00001683 const char *GCCName = getToolChain().getDriver().CCCGenericGCCName.c_str();
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001684 const char *Exec =
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00001685 Args.MakeArgString(getToolChain().GetProgramPath(C, GCCName));
Daniel Dunbardaab7b12009-12-02 03:23:25 +00001686 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001687}
1688
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00001689void gcc::Preprocess::RenderExtraToolArgs(const JobAction &JA,
1690 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001691 CmdArgs.push_back("-E");
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001692}
1693
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00001694void gcc::Precompile::RenderExtraToolArgs(const JobAction &JA,
1695 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001696 // The type is good enough.
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001697}
1698
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00001699void gcc::Compile::RenderExtraToolArgs(const JobAction &JA,
1700 ArgStringList &CmdArgs) const {
Daniel Dunbar64952502010-02-11 03:16:21 +00001701 const Driver &D = getToolChain().getDriver();
1702
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00001703 // If -flto, etc. are present then make sure not to force assembly output.
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00001704 if (JA.getType() == types::TY_LLVM_IR || JA.getType() == types::TY_LTO_IR ||
1705 JA.getType() == types::TY_LLVM_BC || JA.getType() == types::TY_LTO_BC)
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00001706 CmdArgs.push_back("-c");
Daniel Dunbar64952502010-02-11 03:16:21 +00001707 else {
1708 if (JA.getType() != types::TY_PP_Asm)
1709 D.Diag(clang::diag::err_drv_invalid_gcc_output_type)
1710 << getTypeName(JA.getType());
1711
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00001712 CmdArgs.push_back("-S");
Daniel Dunbar64952502010-02-11 03:16:21 +00001713 }
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001714}
1715
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00001716void gcc::Assemble::RenderExtraToolArgs(const JobAction &JA,
1717 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001718 CmdArgs.push_back("-c");
Daniel Dunbar47ac7d22009-03-18 06:00:36 +00001719}
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001720
Daniel Dunbar82b51cc2010-01-25 22:35:08 +00001721void gcc::Link::RenderExtraToolArgs(const JobAction &JA,
1722 ArgStringList &CmdArgs) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +00001723 // The types are (hopefully) good enough.
1724}
1725
Daniel Dunbar40f12652009-03-29 17:08:39 +00001726const char *darwin::CC1::getCC1Name(types::ID Type) const {
1727 switch (Type) {
1728 default:
1729 assert(0 && "Unexpected type for Darwin CC1 tool.");
1730 case types::TY_Asm:
1731 case types::TY_C: case types::TY_CHeader:
1732 case types::TY_PP_C: case types::TY_PP_CHeader:
1733 return "cc1";
1734 case types::TY_ObjC: case types::TY_ObjCHeader:
1735 case types::TY_PP_ObjC: case types::TY_PP_ObjCHeader:
1736 return "cc1obj";
1737 case types::TY_CXX: case types::TY_CXXHeader:
1738 case types::TY_PP_CXX: case types::TY_PP_CXXHeader:
1739 return "cc1plus";
1740 case types::TY_ObjCXX: case types::TY_ObjCXXHeader:
1741 case types::TY_PP_ObjCXX: case types::TY_PP_ObjCXXHeader:
1742 return "cc1objplus";
1743 }
1744}
1745
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001746const char *darwin::CC1::getBaseInputName(const ArgList &Args,
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00001747 const InputInfoList &Inputs) {
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001748 llvm::sys::Path P(Inputs[0].getBaseInput());
Daniel Dunbar88137642009-09-09 22:32:48 +00001749 return Args.MakeArgString(P.getLast());
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001750}
1751
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001752const char *darwin::CC1::getBaseInputStem(const ArgList &Args,
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00001753 const InputInfoList &Inputs) {
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001754 const char *Str = getBaseInputName(Args, Inputs);
1755
1756 if (const char *End = strchr(Str, '.'))
Daniel Dunbar88137642009-09-09 22:32:48 +00001757 return Args.MakeArgString(std::string(Str, End));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001758
1759 return Str;
1760}
1761
1762const char *
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001763darwin::CC1::getDependencyFileName(const ArgList &Args,
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00001764 const InputInfoList &Inputs) {
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001765 // FIXME: Think about this more.
1766 std::string Res;
1767
1768 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
1769 std::string Str(OutputOpt->getValue(Args));
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001770
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001771 Res = Str.substr(0, Str.rfind('.'));
1772 } else
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00001773 Res = darwin::CC1::getBaseInputStem(Args, Inputs);
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001774
Daniel Dunbar88137642009-09-09 22:32:48 +00001775 return Args.MakeArgString(Res + ".d");
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001776}
1777
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001778void darwin::CC1::AddCC1Args(const ArgList &Args,
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001779 ArgStringList &CmdArgs) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00001780 const Driver &D = getToolChain().getDriver();
Daniel Dunbare2fd6642009-09-10 01:21:12 +00001781
1782 CheckCodeGenerationOptions(D, Args);
1783
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001784 // Derived from cc1 spec.
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001785 if (!Args.hasArg(options::OPT_mkernel) && !Args.hasArg(options::OPT_static) &&
1786 !Args.hasArg(options::OPT_mdynamic_no_pic))
1787 CmdArgs.push_back("-fPIC");
1788
Daniel Dunbar2ba91572009-09-10 03:37:02 +00001789 if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
1790 getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
1791 if (!Args.hasArg(options::OPT_fbuiltin_strcat))
1792 CmdArgs.push_back("-fno-builtin-strcat");
1793 if (!Args.hasArg(options::OPT_fbuiltin_strcpy))
1794 CmdArgs.push_back("-fno-builtin-strcpy");
1795 }
1796
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001797 // gcc has some code here to deal with when no -mmacosx-version-min
1798 // and no -miphoneos-version-min is present, but this never happens
1799 // due to tool chain specific argument translation.
1800
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001801 if (Args.hasArg(options::OPT_g_Flag) &&
1802 !Args.hasArg(options::OPT_fno_eliminate_unused_debug_symbols))
1803 CmdArgs.push_back("-feliminate-unused-debug-symbols");
1804}
1805
1806void darwin::CC1::AddCC1OptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
1807 const InputInfoList &Inputs,
1808 const ArgStringList &OutputArgs) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00001809 const Driver &D = getToolChain().getDriver();
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001810
1811 // Derived from cc1_options spec.
1812 if (Args.hasArg(options::OPT_fast) ||
1813 Args.hasArg(options::OPT_fastf) ||
1814 Args.hasArg(options::OPT_fastcp))
1815 CmdArgs.push_back("-O3");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001816
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001817 if (Arg *A = Args.getLastArg(options::OPT_pg))
1818 if (Args.hasArg(options::OPT_fomit_frame_pointer))
1819 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
1820 << A->getAsString(Args) << "-fomit-frame-pointer";
1821
1822 AddCC1Args(Args, CmdArgs);
1823
1824 if (!Args.hasArg(options::OPT_Q))
1825 CmdArgs.push_back("-quiet");
1826
1827 CmdArgs.push_back("-dumpbase");
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00001828 CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001829
1830 Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
1831
1832 Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
1833 Args.AddAllArgs(CmdArgs, options::OPT_a_Group);
1834
1835 // FIXME: The goal is to use the user provided -o if that is our
1836 // final output, otherwise to drive from the original input
1837 // name. Find a clean way to go about this.
1838 if ((Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) &&
1839 Args.hasArg(options::OPT_o)) {
1840 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
1841 CmdArgs.push_back("-auxbase-strip");
1842 CmdArgs.push_back(OutputOpt->getValue(Args));
1843 } else {
1844 CmdArgs.push_back("-auxbase");
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00001845 CmdArgs.push_back(darwin::CC1::getBaseInputStem(Args, Inputs));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001846 }
1847
1848 Args.AddAllArgs(CmdArgs, options::OPT_g_Group);
1849
1850 Args.AddAllArgs(CmdArgs, options::OPT_O);
1851 // FIXME: -Wall is getting some special treatment. Investigate.
1852 Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
1853 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001854 Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001855 options::OPT_trigraphs);
Daniel Dunbara3ff2022009-04-26 01:10:38 +00001856 if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
1857 // Honor -std-default.
1858 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
1859 "-std=", /*Joined=*/true);
1860 }
1861
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001862 if (Args.hasArg(options::OPT_v))
1863 CmdArgs.push_back("-version");
1864 if (Args.hasArg(options::OPT_pg))
1865 CmdArgs.push_back("-p");
1866 Args.AddLastArg(CmdArgs, options::OPT_p);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001867
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001868 // The driver treats -fsyntax-only specially.
Daniel Dunbar2ba91572009-09-10 03:37:02 +00001869 if (getToolChain().getTriple().getArch() == llvm::Triple::arm ||
1870 getToolChain().getTriple().getArch() == llvm::Triple::thumb) {
1871 // Removes -fbuiltin-str{cat,cpy}; these aren't recognized by cc1 but are
1872 // used to inhibit the default -fno-builtin-str{cat,cpy}.
1873 //
1874 // FIXME: Should we grow a better way to deal with "removing" args?
Daniel Dunbarcdd96862009-11-25 11:53:23 +00001875 for (arg_iterator it = Args.filtered_begin(options::OPT_f_Group,
1876 options::OPT_fsyntax_only),
1877 ie = Args.filtered_end(); it != ie; ++it) {
Daniel Dunbar7e4953e2010-06-11 22:00:13 +00001878 if (!(*it)->getOption().matches(options::OPT_fbuiltin_strcat) &&
1879 !(*it)->getOption().matches(options::OPT_fbuiltin_strcpy)) {
1880 (*it)->claim();
1881 (*it)->render(Args, CmdArgs);
Daniel Dunbar2ba91572009-09-10 03:37:02 +00001882 }
1883 }
1884 } else
1885 Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001886
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001887 Args.AddAllArgs(CmdArgs, options::OPT_undef);
1888 if (Args.hasArg(options::OPT_Qn))
1889 CmdArgs.push_back("-fno-ident");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001890
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001891 // FIXME: This isn't correct.
1892 //Args.AddLastArg(CmdArgs, options::OPT__help)
1893 //Args.AddLastArg(CmdArgs, options::OPT__targetHelp)
1894
1895 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
1896
1897 // FIXME: Still don't get what is happening here. Investigate.
1898 Args.AddAllArgs(CmdArgs, options::OPT__param);
1899
1900 if (Args.hasArg(options::OPT_fmudflap) ||
1901 Args.hasArg(options::OPT_fmudflapth)) {
1902 CmdArgs.push_back("-fno-builtin");
1903 CmdArgs.push_back("-fno-merge-constants");
1904 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001905
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001906 if (Args.hasArg(options::OPT_coverage)) {
1907 CmdArgs.push_back("-fprofile-arcs");
1908 CmdArgs.push_back("-ftest-coverage");
1909 }
1910
1911 if (types::isCXX(Inputs[0].getType()))
1912 CmdArgs.push_back("-D__private_extern__=extern");
1913}
1914
1915void darwin::CC1::AddCPPOptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
1916 const InputInfoList &Inputs,
1917 const ArgStringList &OutputArgs) const {
1918 // Derived from cpp_options
1919 AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001920
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001921 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
1922
1923 AddCC1Args(Args, CmdArgs);
1924
1925 // NOTE: The code below has some commonality with cpp_options, but
1926 // in classic gcc style ends up sending things in different
1927 // orders. This may be a good merge candidate once we drop pedantic
1928 // compatibility.
1929
1930 Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001931 Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001932 options::OPT_trigraphs);
Daniel Dunbara3ff2022009-04-26 01:10:38 +00001933 if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
1934 // Honor -std-default.
1935 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
1936 "-std=", /*Joined=*/true);
1937 }
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001938 Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
1939 Args.AddLastArg(CmdArgs, options::OPT_w);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001940
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001941 // The driver treats -fsyntax-only specially.
1942 Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
1943
1944 if (Args.hasArg(options::OPT_g_Group) && !Args.hasArg(options::OPT_g0) &&
1945 !Args.hasArg(options::OPT_fno_working_directory))
1946 CmdArgs.push_back("-fworking-directory");
1947
1948 Args.AddAllArgs(CmdArgs, options::OPT_O);
1949 Args.AddAllArgs(CmdArgs, options::OPT_undef);
1950 if (Args.hasArg(options::OPT_save_temps))
1951 CmdArgs.push_back("-fpch-preprocess");
1952}
1953
Daniel Dunbarc21c4852009-04-08 23:54:23 +00001954void darwin::CC1::AddCPPUniqueOptionsArgs(const ArgList &Args,
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001955 ArgStringList &CmdArgs,
Mike Stump1eb44332009-09-09 15:08:12 +00001956 const InputInfoList &Inputs) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00001957 const Driver &D = getToolChain().getDriver();
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001958
Daniel Dunbar88a3d6c2009-09-10 01:21:05 +00001959 CheckPreprocessingOptions(D, Args);
1960
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001961 // Derived from cpp_unique_options.
Daniel Dunbar88a3d6c2009-09-10 01:21:05 +00001962 // -{C,CC} only with -E is checked in CheckPreprocessingOptions().
1963 Args.AddLastArg(CmdArgs, options::OPT_C);
1964 Args.AddLastArg(CmdArgs, options::OPT_CC);
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001965 if (!Args.hasArg(options::OPT_Q))
1966 CmdArgs.push_back("-quiet");
1967 Args.AddAllArgs(CmdArgs, options::OPT_nostdinc);
Douglas Gregor4c2bcad2010-03-24 20:13:48 +00001968 Args.AddAllArgs(CmdArgs, options::OPT_nostdincxx);
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001969 Args.AddLastArg(CmdArgs, options::OPT_v);
1970 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F);
1971 Args.AddLastArg(CmdArgs, options::OPT_P);
1972
1973 // FIXME: Handle %I properly.
1974 if (getToolChain().getArchName() == "x86_64") {
1975 CmdArgs.push_back("-imultilib");
1976 CmdArgs.push_back("x86_64");
1977 }
1978
1979 if (Args.hasArg(options::OPT_MD)) {
1980 CmdArgs.push_back("-MD");
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00001981 CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001982 }
1983
1984 if (Args.hasArg(options::OPT_MMD)) {
1985 CmdArgs.push_back("-MMD");
Daniel Dunbara5a7bd02009-03-30 00:34:04 +00001986 CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00001987 }
1988
1989 Args.AddLastArg(CmdArgs, options::OPT_M);
1990 Args.AddLastArg(CmdArgs, options::OPT_MM);
1991 Args.AddAllArgs(CmdArgs, options::OPT_MF);
1992 Args.AddLastArg(CmdArgs, options::OPT_MG);
1993 Args.AddLastArg(CmdArgs, options::OPT_MP);
1994 Args.AddAllArgs(CmdArgs, options::OPT_MQ);
1995 Args.AddAllArgs(CmdArgs, options::OPT_MT);
1996 if (!Args.hasArg(options::OPT_M) && !Args.hasArg(options::OPT_MM) &&
1997 (Args.hasArg(options::OPT_MD) || Args.hasArg(options::OPT_MMD))) {
1998 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
1999 CmdArgs.push_back("-MQ");
2000 CmdArgs.push_back(OutputOpt->getValue(Args));
2001 }
2002 }
2003
2004 Args.AddLastArg(CmdArgs, options::OPT_remap);
2005 if (Args.hasArg(options::OPT_g3))
2006 CmdArgs.push_back("-dD");
2007 Args.AddLastArg(CmdArgs, options::OPT_H);
2008
2009 AddCPPArgs(Args, CmdArgs);
2010
2011 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U, options::OPT_A);
2012 Args.AddAllArgs(CmdArgs, options::OPT_i_Group);
2013
2014 for (InputInfoList::const_iterator
2015 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2016 const InputInfo &II = *it;
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002017
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002018 if (II.isPipe())
2019 CmdArgs.push_back("-");
2020 else
2021 CmdArgs.push_back(II.getFilename());
2022 }
2023
2024 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
2025 options::OPT_Xpreprocessor);
2026
2027 if (Args.hasArg(options::OPT_fmudflap)) {
2028 CmdArgs.push_back("-D_MUDFLAP");
2029 CmdArgs.push_back("-include");
2030 CmdArgs.push_back("mf-runtime.h");
2031 }
2032
2033 if (Args.hasArg(options::OPT_fmudflapth)) {
2034 CmdArgs.push_back("-D_MUDFLAP");
2035 CmdArgs.push_back("-D_MUDFLAPTH");
2036 CmdArgs.push_back("-include");
2037 CmdArgs.push_back("mf-runtime.h");
2038 }
2039}
2040
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002041void darwin::CC1::AddCPPArgs(const ArgList &Args,
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002042 ArgStringList &CmdArgs) const {
2043 // Derived from cpp spec.
2044
2045 if (Args.hasArg(options::OPT_static)) {
2046 // The gcc spec is broken here, it refers to dynamic but
2047 // that has been translated. Start by being bug compatible.
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002048
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002049 // if (!Args.hasArg(arglist.parser.dynamicOption))
2050 CmdArgs.push_back("-D__STATIC__");
2051 } else
2052 CmdArgs.push_back("-D__DYNAMIC__");
2053
2054 if (Args.hasArg(options::OPT_pthread))
2055 CmdArgs.push_back("-D_REENTRANT");
2056}
2057
Daniel Dunbar40f12652009-03-29 17:08:39 +00002058void darwin::Preprocess::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002059 Job &Dest, const InputInfo &Output,
2060 const InputInfoList &Inputs,
2061 const ArgList &Args,
Daniel Dunbar40f12652009-03-29 17:08:39 +00002062 const char *LinkingOutput) const {
2063 ArgStringList CmdArgs;
2064
2065 assert(Inputs.size() == 1 && "Unexpected number of inputs!");
2066
2067 CmdArgs.push_back("-E");
2068
2069 if (Args.hasArg(options::OPT_traditional) ||
Daniel Dunbar40f12652009-03-29 17:08:39 +00002070 Args.hasArg(options::OPT_traditional_cpp))
2071 CmdArgs.push_back("-traditional-cpp");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002072
Daniel Dunbar40f12652009-03-29 17:08:39 +00002073 ArgStringList OutputArgs;
2074 if (Output.isFilename()) {
2075 OutputArgs.push_back("-o");
2076 OutputArgs.push_back(Output.getFilename());
2077 } else {
2078 assert(Output.isPipe() && "Unexpected CC1 output.");
2079 }
2080
Daniel Dunbar9120f172009-03-29 22:27:40 +00002081 if (Args.hasArg(options::OPT_E)) {
2082 AddCPPOptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2083 } else {
2084 AddCPPOptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2085 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2086 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002087
Daniel Dunbar8a2073a2009-04-03 01:27:06 +00002088 Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
2089
Daniel Dunbar40f12652009-03-29 17:08:39 +00002090 const char *CC1Name = getCC1Name(Inputs[0].getType());
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002091 const char *Exec =
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00002092 Args.MakeArgString(getToolChain().GetProgramPath(C, CC1Name));
Daniel Dunbardaab7b12009-12-02 03:23:25 +00002093 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar40f12652009-03-29 17:08:39 +00002094}
2095
2096void darwin::Compile::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002097 Job &Dest, const InputInfo &Output,
2098 const InputInfoList &Inputs,
2099 const ArgList &Args,
Daniel Dunbar40f12652009-03-29 17:08:39 +00002100 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00002101 const Driver &D = getToolChain().getDriver();
Daniel Dunbar40f12652009-03-29 17:08:39 +00002102 ArgStringList CmdArgs;
2103
2104 assert(Inputs.size() == 1 && "Unexpected number of inputs!");
2105
2106 types::ID InputType = Inputs[0].getType();
2107 const Arg *A;
Eli Friedmanceb5c5b2009-07-14 21:58:17 +00002108 if ((A = Args.getLastArg(options::OPT_traditional)))
Daniel Dunbar40f12652009-03-29 17:08:39 +00002109 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
2110 << A->getAsString(Args) << "-E";
2111
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00002112 if (JA.getType() == types::TY_LLVM_IR ||
2113 JA.getType() == types::TY_LTO_IR)
Daniel Dunbar40f12652009-03-29 17:08:39 +00002114 CmdArgs.push_back("-emit-llvm");
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00002115 else if (JA.getType() == types::TY_LLVM_BC ||
2116 JA.getType() == types::TY_LTO_BC)
Daniel Dunbar40f12652009-03-29 17:08:39 +00002117 CmdArgs.push_back("-emit-llvm-bc");
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00002118 else if (Output.getType() == types::TY_AST)
2119 D.Diag(clang::diag::err_drv_no_ast_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00002120 << getToolChain().getTripleString();
Daniel Dunbarae24a882010-02-11 17:33:45 +00002121 else if (JA.getType() != types::TY_PP_Asm &&
2122 JA.getType() != types::TY_PCH)
Daniel Dunbar64952502010-02-11 03:16:21 +00002123 D.Diag(clang::diag::err_drv_invalid_gcc_output_type)
2124 << getTypeName(JA.getType());
Daniel Dunbar40f12652009-03-29 17:08:39 +00002125
2126 ArgStringList OutputArgs;
2127 if (Output.getType() != types::TY_PCH) {
2128 OutputArgs.push_back("-o");
2129 if (Output.isPipe())
2130 OutputArgs.push_back("-");
2131 else if (Output.isNothing())
2132 OutputArgs.push_back("/dev/null");
2133 else
2134 OutputArgs.push_back(Output.getFilename());
2135 }
2136
2137 // There is no need for this level of compatibility, but it makes
2138 // diffing easier.
2139 bool OutputArgsEarly = (Args.hasArg(options::OPT_fsyntax_only) ||
2140 Args.hasArg(options::OPT_S));
2141
2142 if (types::getPreprocessedType(InputType) != types::TY_INVALID) {
Daniel Dunbara3ec60e2009-03-29 18:40:18 +00002143 AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
Daniel Dunbar40f12652009-03-29 17:08:39 +00002144 if (OutputArgsEarly) {
2145 AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2146 } else {
2147 AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2148 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2149 }
2150 } else {
2151 CmdArgs.push_back("-fpreprocessed");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002152
Daniel Dunbar40f12652009-03-29 17:08:39 +00002153 for (InputInfoList::const_iterator
2154 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2155 const InputInfo &II = *it;
2156
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00002157 // Reject AST inputs.
2158 if (II.getType() == types::TY_AST) {
2159 D.Diag(clang::diag::err_drv_no_ast_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00002160 << getToolChain().getTripleString();
Daniel Dunbar5915fbf2009-09-01 16:57:46 +00002161 return;
2162 }
2163
Daniel Dunbar40f12652009-03-29 17:08:39 +00002164 if (II.isPipe())
2165 CmdArgs.push_back("-");
2166 else
2167 CmdArgs.push_back(II.getFilename());
2168 }
2169
2170 if (OutputArgsEarly) {
2171 AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
2172 } else {
2173 AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
2174 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
2175 }
2176 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002177
Daniel Dunbar40f12652009-03-29 17:08:39 +00002178 if (Output.getType() == types::TY_PCH) {
2179 assert(Output.isFilename() && "Invalid PCH output.");
2180
2181 CmdArgs.push_back("-o");
2182 // NOTE: gcc uses a temp .s file for this, but there doesn't seem
2183 // to be a good reason.
2184 CmdArgs.push_back("/dev/null");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002185
Daniel Dunbare6c9ae12009-11-21 02:31:29 +00002186 CmdArgs.push_back("--output-pch=");
Daniel Dunbar40f12652009-03-29 17:08:39 +00002187 CmdArgs.push_back(Output.getFilename());
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002188 }
Daniel Dunbar40f12652009-03-29 17:08:39 +00002189
2190 const char *CC1Name = getCC1Name(Inputs[0].getType());
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002191 const char *Exec =
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00002192 Args.MakeArgString(getToolChain().GetProgramPath(C, CC1Name));
Daniel Dunbardaab7b12009-12-02 03:23:25 +00002193 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar40f12652009-03-29 17:08:39 +00002194}
2195
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00002196void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002197 Job &Dest, const InputInfo &Output,
2198 const InputInfoList &Inputs,
2199 const ArgList &Args,
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00002200 const char *LinkingOutput) const {
2201 ArgStringList CmdArgs;
2202
2203 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
2204 const InputInfo &Input = Inputs[0];
2205
2206 // Bit of a hack, this is only used for original inputs.
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002207 //
Daniel Dunbar8e4fea62009-04-01 00:27:44 +00002208 // FIXME: This is broken for preprocessed .s inputs.
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00002209 if (Input.isFilename() &&
Daniel Dunbar8e4fea62009-04-01 00:27:44 +00002210 strcmp(Input.getFilename(), Input.getBaseInput()) == 0) {
2211 if (Args.hasArg(options::OPT_gstabs))
2212 CmdArgs.push_back("--gstabs");
2213 else if (Args.hasArg(options::OPT_g_Group))
2214 CmdArgs.push_back("--gdwarf2");
2215 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002216
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00002217 // Derived from asm spec.
Daniel Dunbarcc6f8032009-09-09 18:36:27 +00002218 AddDarwinArch(Args, CmdArgs);
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00002219
Daniel Dunbar251ca6c2010-01-27 00:56:37 +00002220 if (!getDarwinToolChain().isTargetIPhoneOS() ||
Daniel Dunbarcc6f8032009-09-09 18:36:27 +00002221 Args.hasArg(options::OPT_force__cpusubtype__ALL))
2222 CmdArgs.push_back("-force_cpusubtype_ALL");
2223
Daniel Dunbar0e2679d2009-08-24 22:26:16 +00002224 if (getToolChain().getTriple().getArch() != llvm::Triple::x86_64 &&
2225 (Args.hasArg(options::OPT_mkernel) ||
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00002226 Args.hasArg(options::OPT_static) ||
Daniel Dunbar0e2679d2009-08-24 22:26:16 +00002227 Args.hasArg(options::OPT_fapple_kext)))
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002228 CmdArgs.push_back("-static");
2229
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00002230 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
2231 options::OPT_Xassembler);
2232
2233 assert(Output.isFilename() && "Unexpected lipo output.");
2234 CmdArgs.push_back("-o");
2235 CmdArgs.push_back(Output.getFilename());
2236
2237 if (Input.isPipe()) {
2238 CmdArgs.push_back("-");
2239 } else {
2240 assert(Input.isFilename() && "Invalid input.");
2241 CmdArgs.push_back(Input.getFilename());
2242 }
2243
2244 // asm_final spec is empty.
2245
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002246 const char *Exec =
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00002247 Args.MakeArgString(getToolChain().GetProgramPath(C, "as"));
Daniel Dunbardaab7b12009-12-02 03:23:25 +00002248 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar8cac5f72009-03-20 16:06:39 +00002249}
Daniel Dunbarff7488d2009-03-20 00:52:38 +00002250
Daniel Dunbarfbefe6b2009-09-09 18:36:20 +00002251void darwin::DarwinTool::AddDarwinArch(const ArgList &Args,
2252 ArgStringList &CmdArgs) const {
Daniel Dunbareeff4062010-01-22 02:04:58 +00002253 llvm::StringRef ArchName = getDarwinToolChain().getDarwinArchName(Args);
2254
Daniel Dunbar02633b52009-03-26 16:23:12 +00002255 // Derived from darwin_arch spec.
2256 CmdArgs.push_back("-arch");
Daniel Dunbareeff4062010-01-22 02:04:58 +00002257 CmdArgs.push_back(Args.MakeArgString(ArchName));
Daniel Dunbar78dbd582009-09-04 18:35:31 +00002258
Daniel Dunbareeff4062010-01-22 02:04:58 +00002259 // FIXME: Is this needed anymore?
2260 if (ArchName == "arm")
Daniel Dunbar78dbd582009-09-04 18:35:31 +00002261 CmdArgs.push_back("-force_cpusubtype_ALL");
Daniel Dunbar02633b52009-03-26 16:23:12 +00002262}
2263
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002264void darwin::Link::AddLinkArgs(const ArgList &Args,
Daniel Dunbar02633b52009-03-26 16:23:12 +00002265 ArgStringList &CmdArgs) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00002266 const Driver &D = getToolChain().getDriver();
Daniel Dunbar02633b52009-03-26 16:23:12 +00002267
2268 // Derived from the "link" spec.
2269 Args.AddAllArgs(CmdArgs, options::OPT_static);
2270 if (!Args.hasArg(options::OPT_static))
2271 CmdArgs.push_back("-dynamic");
2272 if (Args.hasArg(options::OPT_fgnu_runtime)) {
2273 // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu
2274 // here. How do we wish to handle such things?
2275 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002276
Daniel Dunbar02633b52009-03-26 16:23:12 +00002277 if (!Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbara6d38492010-01-22 02:04:52 +00002278 AddDarwinArch(Args, CmdArgs);
Daniel Dunbara6d38492010-01-22 02:04:52 +00002279 // FIXME: Why do this only on this path?
Daniel Dunbar8917dd42010-01-22 03:37:33 +00002280 Args.AddLastArg(CmdArgs, options::OPT_force__cpusubtype__ALL);
Daniel Dunbar02633b52009-03-26 16:23:12 +00002281
2282 Args.AddLastArg(CmdArgs, options::OPT_bundle);
2283 Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader);
2284 Args.AddAllArgs(CmdArgs, options::OPT_client__name);
2285
2286 Arg *A;
2287 if ((A = Args.getLastArg(options::OPT_compatibility__version)) ||
2288 (A = Args.getLastArg(options::OPT_current__version)) ||
2289 (A = Args.getLastArg(options::OPT_install__name)))
2290 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
2291 << A->getAsString(Args) << "-dynamiclib";
2292
2293 Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace);
2294 Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs);
2295 Args.AddLastArg(CmdArgs, options::OPT_private__bundle);
2296 } else {
2297 CmdArgs.push_back("-dylib");
2298
2299 Arg *A;
2300 if ((A = Args.getLastArg(options::OPT_bundle)) ||
2301 (A = Args.getLastArg(options::OPT_bundle__loader)) ||
2302 (A = Args.getLastArg(options::OPT_client__name)) ||
2303 (A = Args.getLastArg(options::OPT_force__flat__namespace)) ||
2304 (A = Args.getLastArg(options::OPT_keep__private__externs)) ||
2305 (A = Args.getLastArg(options::OPT_private__bundle)))
2306 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
2307 << A->getAsString(Args) << "-dynamiclib";
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002308
Daniel Dunbar02633b52009-03-26 16:23:12 +00002309 Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version,
2310 "-dylib_compatibility_version");
2311 Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version,
2312 "-dylib_current_version");
2313
Daniel Dunbara6d38492010-01-22 02:04:52 +00002314 AddDarwinArch(Args, CmdArgs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00002315
2316 Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name,
2317 "-dylib_install_name");
2318 }
2319
2320 Args.AddLastArg(CmdArgs, options::OPT_all__load);
2321 Args.AddAllArgs(CmdArgs, options::OPT_allowable__client);
2322 Args.AddLastArg(CmdArgs, options::OPT_bind__at__load);
Daniel Dunbar251ca6c2010-01-27 00:56:37 +00002323 if (getDarwinToolChain().isTargetIPhoneOS())
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00002324 Args.AddLastArg(CmdArgs, options::OPT_arch__errors__fatal);
Daniel Dunbar02633b52009-03-26 16:23:12 +00002325 Args.AddLastArg(CmdArgs, options::OPT_dead__strip);
2326 Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms);
2327 Args.AddAllArgs(CmdArgs, options::OPT_dylib__file);
2328 Args.AddLastArg(CmdArgs, options::OPT_dynamic);
2329 Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list);
2330 Args.AddLastArg(CmdArgs, options::OPT_flat__namespace);
2331 Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names);
2332 Args.AddAllArgs(CmdArgs, options::OPT_image__base);
2333 Args.AddAllArgs(CmdArgs, options::OPT_init);
2334
Daniel Dunbar314cba02010-01-26 03:56:40 +00002335 // Adding all arguments doesn't make sense here but this is what gcc does. One
2336 // of this should always be present thanks to argument translation.
2337 assert((Args.hasArg(options::OPT_mmacosx_version_min_EQ) ||
2338 Args.hasArg(options::OPT_miphoneos_version_min_EQ)) &&
2339 "Missing version argument (lost in translation)?");
Daniel Dunbar30392de2009-09-04 18:35:21 +00002340 Args.AddAllArgsTranslated(CmdArgs, options::OPT_mmacosx_version_min_EQ,
Daniel Dunbar294691e2009-11-04 06:24:38 +00002341 "-macosx_version_min");
Daniel Dunbar02633b52009-03-26 16:23:12 +00002342 Args.AddAllArgsTranslated(CmdArgs, options::OPT_miphoneos_version_min_EQ,
2343 "-iphoneos_version_min");
2344 Args.AddLastArg(CmdArgs, options::OPT_nomultidefs);
2345 Args.AddLastArg(CmdArgs, options::OPT_multi__module);
2346 Args.AddLastArg(CmdArgs, options::OPT_single__module);
2347 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined);
2348 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002349
Daniel Dunbar02633b52009-03-26 16:23:12 +00002350 if (Args.hasArg(options::OPT_fpie))
2351 CmdArgs.push_back("-pie");
2352
2353 Args.AddLastArg(CmdArgs, options::OPT_prebind);
2354 Args.AddLastArg(CmdArgs, options::OPT_noprebind);
2355 Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding);
2356 Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules);
2357 Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs);
2358 Args.AddAllArgs(CmdArgs, options::OPT_sectcreate);
2359 Args.AddAllArgs(CmdArgs, options::OPT_sectorder);
2360 Args.AddAllArgs(CmdArgs, options::OPT_seg1addr);
2361 Args.AddAllArgs(CmdArgs, options::OPT_segprot);
2362 Args.AddAllArgs(CmdArgs, options::OPT_segaddr);
2363 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr);
2364 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr);
2365 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table);
2366 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename);
2367 Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
2368 Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00002369
Daniel Dunbar02633b52009-03-26 16:23:12 +00002370 Args.AddAllArgsTranslated(CmdArgs, options::OPT_isysroot, "-syslibroot");
Daniel Dunbar251ca6c2010-01-27 00:56:37 +00002371 if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00002372 if (!Args.hasArg(options::OPT_isysroot)) {
2373 CmdArgs.push_back("-syslibroot");
2374 CmdArgs.push_back("/Developer/SDKs/Extra");
2375 }
2376 }
2377
Daniel Dunbar02633b52009-03-26 16:23:12 +00002378 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);
2379 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints);
2380 Args.AddAllArgs(CmdArgs, options::OPT_umbrella);
2381 Args.AddAllArgs(CmdArgs, options::OPT_undefined);
2382 Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list);
Daniel Dunbard82f8fa2009-09-04 18:35:41 +00002383 Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches);
Daniel Dunbar02633b52009-03-26 16:23:12 +00002384 Args.AddLastArg(CmdArgs, options::OPT_X_Flag);
2385 Args.AddAllArgs(CmdArgs, options::OPT_y);
2386 Args.AddLastArg(CmdArgs, options::OPT_w);
2387 Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size);
2388 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__);
2389 Args.AddLastArg(CmdArgs, options::OPT_seglinkedit);
2390 Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit);
2391 Args.AddAllArgs(CmdArgs, options::OPT_sectalign);
2392 Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols);
2393 Args.AddAllArgs(CmdArgs, options::OPT_segcreate);
2394 Args.AddLastArg(CmdArgs, options::OPT_whyload);
2395 Args.AddLastArg(CmdArgs, options::OPT_whatsloaded);
2396 Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name);
2397 Args.AddLastArg(CmdArgs, options::OPT_dylinker);
2398 Args.AddLastArg(CmdArgs, options::OPT_Mach);
2399}
2400
2401void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002402 Job &Dest, const InputInfo &Output,
2403 const InputInfoList &Inputs,
2404 const ArgList &Args,
Daniel Dunbar02633b52009-03-26 16:23:12 +00002405 const char *LinkingOutput) const {
2406 assert(Output.getType() == types::TY_Image && "Invalid linker output type.");
Daniel Dunbare0be8b12009-09-08 16:39:16 +00002407
Daniel Dunbar02633b52009-03-26 16:23:12 +00002408 // The logic here is derived from gcc's behavior; most of which
2409 // comes from specs (starting with link_command). Consult gcc for
2410 // more information.
Daniel Dunbar02633b52009-03-26 16:23:12 +00002411 ArgStringList CmdArgs;
2412
2413 // I'm not sure why this particular decomposition exists in gcc, but
2414 // we follow suite for ease of comparison.
2415 AddLinkArgs(Args, CmdArgs);
2416
Daniel Dunbar02633b52009-03-26 16:23:12 +00002417 Args.AddAllArgs(CmdArgs, options::OPT_d_Flag);
2418 Args.AddAllArgs(CmdArgs, options::OPT_s);
2419 Args.AddAllArgs(CmdArgs, options::OPT_t);
2420 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
2421 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
2422 Args.AddAllArgs(CmdArgs, options::OPT_A);
2423 Args.AddLastArg(CmdArgs, options::OPT_e);
2424 Args.AddAllArgs(CmdArgs, options::OPT_m_Separate);
2425 Args.AddAllArgs(CmdArgs, options::OPT_r);
2426
Daniel Dunbar02633b52009-03-26 16:23:12 +00002427 CmdArgs.push_back("-o");
2428 CmdArgs.push_back(Output.getFilename());
2429
Daniel Dunbar02633b52009-03-26 16:23:12 +00002430 if (!Args.hasArg(options::OPT_A) &&
2431 !Args.hasArg(options::OPT_nostdlib) &&
2432 !Args.hasArg(options::OPT_nostartfiles)) {
2433 // Derived from startfile spec.
2434 if (Args.hasArg(options::OPT_dynamiclib)) {
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002435 // Derived from darwin_dylib1 spec.
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00002436 if (getDarwinToolChain().isTargetIPhoneOS()) {
2437 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
2438 CmdArgs.push_back("-ldylib1.o");
2439 } else {
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00002440 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00002441 CmdArgs.push_back("-ldylib1.o");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00002442 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00002443 CmdArgs.push_back("-ldylib1.10.5.o");
2444 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00002445 } else {
2446 if (Args.hasArg(options::OPT_bundle)) {
Daniel Dunbar8a8d8af2009-04-01 03:17:40 +00002447 if (!Args.hasArg(options::OPT_static)) {
2448 // Derived from darwin_bundle1 spec.
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00002449 if (getDarwinToolChain().isTargetIPhoneOS()) {
2450 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
2451 CmdArgs.push_back("-lbundle1.o");
2452 } else {
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00002453 if (getDarwinToolChain().isMacosxVersionLT(10, 6))
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00002454 CmdArgs.push_back("-lbundle1.o");
2455 }
Daniel Dunbar8a8d8af2009-04-01 03:17:40 +00002456 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00002457 } else {
2458 if (Args.hasArg(options::OPT_pg)) {
2459 if (Args.hasArg(options::OPT_static) ||
2460 Args.hasArg(options::OPT_object) ||
2461 Args.hasArg(options::OPT_preload)) {
2462 CmdArgs.push_back("-lgcrt0.o");
2463 } else {
2464 CmdArgs.push_back("-lgcrt1.o");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002465
Daniel Dunbar02633b52009-03-26 16:23:12 +00002466 // darwin_crt2 spec is empty.
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002467 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00002468 } else {
2469 if (Args.hasArg(options::OPT_static) ||
2470 Args.hasArg(options::OPT_object) ||
2471 Args.hasArg(options::OPT_preload)) {
2472 CmdArgs.push_back("-lcrt0.o");
2473 } else {
2474 // Derived from darwin_crt1 spec.
Daniel Dunbar251ca6c2010-01-27 00:56:37 +00002475 if (getDarwinToolChain().isTargetIPhoneOS()) {
Daniel Dunbarcacb0f02010-01-27 00:56:56 +00002476 if (getDarwinToolChain().isIPhoneOSVersionLT(3, 1))
2477 CmdArgs.push_back("-lcrt1.o");
2478 else
2479 CmdArgs.push_back("-lcrt1.3.1.o");
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00002480 } else {
2481 if (getDarwinToolChain().isMacosxVersionLT(10, 5))
2482 CmdArgs.push_back("-lcrt1.o");
2483 else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
2484 CmdArgs.push_back("-lcrt1.10.5.o");
2485 else
2486 CmdArgs.push_back("-lcrt1.10.6.o");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002487
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00002488 // darwin_crt2 spec is empty.
2489 }
Daniel Dunbar02633b52009-03-26 16:23:12 +00002490 }
2491 }
2492 }
2493 }
2494
Daniel Dunbarce3fdf22010-01-27 00:57:03 +00002495 if (!getDarwinToolChain().isTargetIPhoneOS() &&
2496 Args.hasArg(options::OPT_shared_libgcc) &&
2497 getDarwinToolChain().isMacosxVersionLT(10, 5)) {
Daniel Dunbar88137642009-09-09 22:32:48 +00002498 const char *Str =
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00002499 Args.MakeArgString(getToolChain().GetFilePath(C, "crt3.o"));
Daniel Dunbar88137642009-09-09 22:32:48 +00002500 CmdArgs.push_back(Str);
Daniel Dunbar02633b52009-03-26 16:23:12 +00002501 }
2502 }
2503
2504 Args.AddAllArgs(CmdArgs, options::OPT_L);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002505
Daniel Dunbar02633b52009-03-26 16:23:12 +00002506 if (Args.hasArg(options::OPT_fopenmp))
2507 // This is more complicated in gcc...
2508 CmdArgs.push_back("-lgomp");
2509
Daniel Dunbar6b200b22009-09-18 08:14:36 +00002510 getDarwinToolChain().AddLinkSearchPathArgs(Args, CmdArgs);
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002511
Daniel Dunbar02633b52009-03-26 16:23:12 +00002512 for (InputInfoList::const_iterator
2513 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2514 const InputInfo &II = *it;
2515 if (II.isFilename())
2516 CmdArgs.push_back(II.getFilename());
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002517 else
Daniel Dunbar02633b52009-03-26 16:23:12 +00002518 II.getInputArg().renderAsInput(Args, CmdArgs);
2519 }
2520
2521 if (LinkingOutput) {
2522 CmdArgs.push_back("-arch_multiple");
2523 CmdArgs.push_back("-final_output");
2524 CmdArgs.push_back(LinkingOutput);
2525 }
2526
2527 if (Args.hasArg(options::OPT_fprofile_arcs) ||
2528 Args.hasArg(options::OPT_fprofile_generate) ||
2529 Args.hasArg(options::OPT_fcreate_profile) ||
2530 Args.hasArg(options::OPT_coverage))
2531 CmdArgs.push_back("-lgcov");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002532
Daniel Dunbar02633b52009-03-26 16:23:12 +00002533 if (Args.hasArg(options::OPT_fnested_functions))
2534 CmdArgs.push_back("-allow_stack_execute");
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002535
Daniel Dunbar02633b52009-03-26 16:23:12 +00002536 if (!Args.hasArg(options::OPT_nostdlib) &&
2537 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbaredfa02b2009-04-08 06:06:21 +00002538 // FIXME: g++ is more complicated here, it tries to put -lstdc++
2539 // before -lm, for example.
Daniel Dunbaree788e72009-12-21 18:54:17 +00002540 if (getToolChain().getDriver().CCCIsCXX)
Daniel Dunbaredfa02b2009-04-08 06:06:21 +00002541 CmdArgs.push_back("-lstdc++");
2542
Daniel Dunbar02633b52009-03-26 16:23:12 +00002543 // link_ssp spec is empty.
2544
Daniel Dunbar6cd41542009-09-18 08:15:03 +00002545 // Let the tool chain choose which runtime library to link.
2546 getDarwinToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs);
Daniel Dunbar02633b52009-03-26 16:23:12 +00002547 }
2548
2549 if (!Args.hasArg(options::OPT_A) &&
2550 !Args.hasArg(options::OPT_nostdlib) &&
2551 !Args.hasArg(options::OPT_nostartfiles)) {
2552 // endfile_spec is empty.
2553 }
2554
2555 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
2556 Args.AddAllArgs(CmdArgs, options::OPT_F);
2557
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002558 const char *Exec =
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00002559 Args.MakeArgString(getToolChain().GetProgramPath(C, "ld"));
Daniel Dunbardaab7b12009-12-02 03:23:25 +00002560 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar02633b52009-03-26 16:23:12 +00002561}
2562
Daniel Dunbarff7488d2009-03-20 00:52:38 +00002563void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002564 Job &Dest, const InputInfo &Output,
2565 const InputInfoList &Inputs,
2566 const ArgList &Args,
Daniel Dunbarff7488d2009-03-20 00:52:38 +00002567 const char *LinkingOutput) const {
2568 ArgStringList CmdArgs;
2569
2570 CmdArgs.push_back("-create");
2571 assert(Output.isFilename() && "Unexpected lipo output.");
Daniel Dunbara428df82009-03-24 00:24:37 +00002572
2573 CmdArgs.push_back("-output");
Daniel Dunbarff7488d2009-03-20 00:52:38 +00002574 CmdArgs.push_back(Output.getFilename());
Daniel Dunbara428df82009-03-24 00:24:37 +00002575
Daniel Dunbarff7488d2009-03-20 00:52:38 +00002576 for (InputInfoList::const_iterator
2577 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2578 const InputInfo &II = *it;
2579 assert(II.isFilename() && "Unexpected lipo input.");
2580 CmdArgs.push_back(II.getFilename());
2581 }
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002582 const char *Exec =
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00002583 Args.MakeArgString(getToolChain().GetProgramPath(C, "lipo"));
Daniel Dunbardaab7b12009-12-02 03:23:25 +00002584 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarff7488d2009-03-20 00:52:38 +00002585}
Daniel Dunbar68a31d42009-03-31 17:45:15 +00002586
Daniel Dunbar6e0f2542010-06-04 18:28:36 +00002587void darwin::Dsymutil::ConstructJob(Compilation &C, const JobAction &JA,
2588 Job &Dest, const InputInfo &Output,
2589 const InputInfoList &Inputs,
2590 const ArgList &Args,
2591 const char *LinkingOutput) const {
2592 ArgStringList CmdArgs;
2593
2594 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
2595 const InputInfo &Input = Inputs[0];
2596 assert(Input.isFilename() && "Unexpected dsymutil input.");
2597 CmdArgs.push_back(Input.getFilename());
2598
2599 CmdArgs.push_back("-o");
2600 CmdArgs.push_back(Output.getFilename());
2601
2602 const char *Exec =
2603 Args.MakeArgString(getToolChain().GetProgramPath(C, "dsymutil"));
2604 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
2605}
2606
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002607void auroraux::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar294691e2009-11-04 06:24:38 +00002608 Job &Dest, const InputInfo &Output,
2609 const InputInfoList &Inputs,
2610 const ArgList &Args,
2611 const char *LinkingOutput) const {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002612 ArgStringList CmdArgs;
2613
2614 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
2615 options::OPT_Xassembler);
2616
2617 CmdArgs.push_back("-o");
2618 if (Output.isPipe())
2619 CmdArgs.push_back("-");
2620 else
2621 CmdArgs.push_back(Output.getFilename());
2622
2623 for (InputInfoList::const_iterator
2624 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2625 const InputInfo &II = *it;
2626 if (II.isPipe())
2627 CmdArgs.push_back("-");
2628 else
2629 CmdArgs.push_back(II.getFilename());
2630 }
2631
2632 const char *Exec =
Edward O'Callaghanff430e62009-10-19 07:02:08 +00002633 Args.MakeArgString(getToolChain().GetProgramPath(C, "gas"));
Daniel Dunbardaab7b12009-12-02 03:23:25 +00002634 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002635}
2636
2637void auroraux::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar294691e2009-11-04 06:24:38 +00002638 Job &Dest, const InputInfo &Output,
2639 const InputInfoList &Inputs,
2640 const ArgList &Args,
2641 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00002642 const Driver &D = getToolChain().getDriver();
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002643 ArgStringList CmdArgs;
2644
2645 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar294691e2009-11-04 06:24:38 +00002646 (!Args.hasArg(options::OPT_shared))) {
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002647 CmdArgs.push_back("-e");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00002648 CmdArgs.push_back("_start");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002649 }
2650
2651 if (Args.hasArg(options::OPT_static)) {
2652 CmdArgs.push_back("-Bstatic");
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00002653 CmdArgs.push_back("-dn");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002654 } else {
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00002655// CmdArgs.push_back("--eh-frame-hdr");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002656 CmdArgs.push_back("-Bdynamic");
2657 if (Args.hasArg(options::OPT_shared)) {
2658 CmdArgs.push_back("-shared");
2659 } else {
Edward O'Callaghan3cecc192009-10-16 19:44:18 +00002660 CmdArgs.push_back("--dynamic-linker");
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002661 CmdArgs.push_back("/lib/ld.so.1"); // 64Bit Path /lib/amd64/ld.so.1
2662 }
2663 }
2664
2665 if (Output.isPipe()) {
2666 CmdArgs.push_back("-o");
2667 CmdArgs.push_back("-");
2668 } else if (Output.isFilename()) {
2669 CmdArgs.push_back("-o");
2670 CmdArgs.push_back(Output.getFilename());
2671 } else {
2672 assert(Output.isNothing() && "Invalid output.");
2673 }
2674
2675 if (!Args.hasArg(options::OPT_nostdlib) &&
2676 !Args.hasArg(options::OPT_nostartfiles)) {
2677 if (!Args.hasArg(options::OPT_shared)) {
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00002678 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crt1.o")));
2679 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crti.o")));
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00002680 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtbegin.o")));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002681 } else {
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00002682 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crti.o")));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002683 }
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00002684 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtn.o")));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002685 }
2686
Daniel Dunbar294691e2009-11-04 06:24:38 +00002687 CmdArgs.push_back(Args.MakeArgString("-L/opt/gcc4/lib/gcc/"
2688 + getToolChain().getTripleString()
Daniel Dunbarf7fb31f2009-10-29 02:24:37 +00002689 + "/4.2.4"));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002690
2691 Args.AddAllArgs(CmdArgs, options::OPT_L);
2692 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
2693 Args.AddAllArgs(CmdArgs, options::OPT_e);
2694
2695 for (InputInfoList::const_iterator
2696 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2697 const InputInfo &II = *it;
2698
2699 // Don't try to pass LLVM inputs to a generic gcc.
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00002700 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
2701 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002702 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00002703 << getToolChain().getTripleString();
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002704
2705 if (II.isPipe())
2706 CmdArgs.push_back("-");
2707 else if (II.isFilename())
2708 CmdArgs.push_back(II.getFilename());
2709 else
2710 II.getInputArg().renderAsInput(Args, CmdArgs);
2711 }
2712
2713 if (!Args.hasArg(options::OPT_nostdlib) &&
2714 !Args.hasArg(options::OPT_nodefaultlibs)) {
2715 // FIXME: For some reason GCC passes -lgcc before adding
2716 // the default system libraries. Just mimic this for now.
2717 CmdArgs.push_back("-lgcc");
2718
2719 if (Args.hasArg(options::OPT_pthread))
2720 CmdArgs.push_back("-pthread");
2721 if (!Args.hasArg(options::OPT_shared))
2722 CmdArgs.push_back("-lc");
2723 CmdArgs.push_back("-lgcc");
2724 }
2725
2726 if (!Args.hasArg(options::OPT_nostdlib) &&
2727 !Args.hasArg(options::OPT_nostartfiles)) {
2728 if (!Args.hasArg(options::OPT_shared))
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00002729 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtend.o")));
Edward O'Callaghan7adf9492009-10-15 07:44:07 +00002730// else
2731// CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtendS.o")));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002732 }
2733
2734 const char *Exec =
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00002735 Args.MakeArgString(getToolChain().GetProgramPath(C, "ld"));
Daniel Dunbardaab7b12009-12-02 03:23:25 +00002736 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
Edward O'Callaghane7925a02009-08-22 01:06:46 +00002737}
2738
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002739void openbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
2740 Job &Dest, const InputInfo &Output,
2741 const InputInfoList &Inputs,
2742 const ArgList &Args,
Mike Stump1eb44332009-09-09 15:08:12 +00002743 const char *LinkingOutput) const {
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002744 ArgStringList CmdArgs;
2745
2746 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
2747 options::OPT_Xassembler);
2748
2749 CmdArgs.push_back("-o");
2750 if (Output.isPipe())
2751 CmdArgs.push_back("-");
2752 else
2753 CmdArgs.push_back(Output.getFilename());
2754
2755 for (InputInfoList::const_iterator
2756 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2757 const InputInfo &II = *it;
2758 if (II.isPipe())
2759 CmdArgs.push_back("-");
2760 else
2761 CmdArgs.push_back(II.getFilename());
2762 }
2763
2764 const char *Exec =
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00002765 Args.MakeArgString(getToolChain().GetProgramPath(C, "as"));
Daniel Dunbardaab7b12009-12-02 03:23:25 +00002766 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002767}
2768
2769void openbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
2770 Job &Dest, const InputInfo &Output,
2771 const InputInfoList &Inputs,
2772 const ArgList &Args,
2773 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00002774 const Driver &D = getToolChain().getDriver();
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002775 ArgStringList CmdArgs;
2776
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00002777 if ((!Args.hasArg(options::OPT_nostdlib)) &&
Daniel Dunbar294691e2009-11-04 06:24:38 +00002778 (!Args.hasArg(options::OPT_shared))) {
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00002779 CmdArgs.push_back("-e");
2780 CmdArgs.push_back("__start");
2781 }
2782
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002783 if (Args.hasArg(options::OPT_static)) {
2784 CmdArgs.push_back("-Bstatic");
2785 } else {
2786 CmdArgs.push_back("--eh-frame-hdr");
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00002787 CmdArgs.push_back("-Bdynamic");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002788 if (Args.hasArg(options::OPT_shared)) {
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00002789 CmdArgs.push_back("-shared");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002790 } else {
2791 CmdArgs.push_back("-dynamic-linker");
2792 CmdArgs.push_back("/usr/libexec/ld.so");
2793 }
2794 }
2795
2796 if (Output.isPipe()) {
2797 CmdArgs.push_back("-o");
2798 CmdArgs.push_back("-");
2799 } else if (Output.isFilename()) {
2800 CmdArgs.push_back("-o");
2801 CmdArgs.push_back(Output.getFilename());
2802 } else {
2803 assert(Output.isNothing() && "Invalid output.");
2804 }
2805
2806 if (!Args.hasArg(options::OPT_nostdlib) &&
2807 !Args.hasArg(options::OPT_nostartfiles)) {
2808 if (!Args.hasArg(options::OPT_shared)) {
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00002809 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crt0.o")));
2810 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtbegin.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002811 } else {
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00002812 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtbeginS.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002813 }
2814 }
2815
Edward O'Callaghane7e18202009-10-28 15:13:08 +00002816 std::string Triple = getToolChain().getTripleString();
2817 if (Triple.substr(0, 6) == "x86_64")
Daniel Dunbar294691e2009-11-04 06:24:38 +00002818 Triple.replace(0, 6, "amd64");
Daniel Dunbarf7fb31f2009-10-29 02:24:37 +00002819 CmdArgs.push_back(Args.MakeArgString("-L/usr/lib/gcc-lib/" + Triple +
2820 "/3.3.5"));
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00002821
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002822 Args.AddAllArgs(CmdArgs, options::OPT_L);
2823 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
2824 Args.AddAllArgs(CmdArgs, options::OPT_e);
2825
2826 for (InputInfoList::const_iterator
2827 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2828 const InputInfo &II = *it;
2829
2830 // Don't try to pass LLVM inputs to a generic gcc.
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00002831 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
2832 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002833 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00002834 << getToolChain().getTripleString();
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002835
2836 if (II.isPipe())
2837 CmdArgs.push_back("-");
2838 else if (II.isFilename())
2839 CmdArgs.push_back(II.getFilename());
2840 else
2841 II.getInputArg().renderAsInput(Args, CmdArgs);
2842 }
2843
2844 if (!Args.hasArg(options::OPT_nostdlib) &&
2845 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00002846 // FIXME: For some reason GCC passes -lgcc before adding
2847 // the default system libraries. Just mimic this for now.
2848 CmdArgs.push_back("-lgcc");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002849
2850 if (Args.hasArg(options::OPT_pthread))
2851 CmdArgs.push_back("-pthread");
Daniel Dunbar2bbcf662009-08-03 01:28:59 +00002852 if (!Args.hasArg(options::OPT_shared))
2853 CmdArgs.push_back("-lc");
2854 CmdArgs.push_back("-lgcc");
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002855 }
2856
2857 if (!Args.hasArg(options::OPT_nostdlib) &&
2858 !Args.hasArg(options::OPT_nostartfiles)) {
2859 if (!Args.hasArg(options::OPT_shared))
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00002860 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtend.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002861 else
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00002862 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtendS.o")));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002863 }
2864
2865 const char *Exec =
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00002866 Args.MakeArgString(getToolChain().GetProgramPath(C, "ld"));
Daniel Dunbardaab7b12009-12-02 03:23:25 +00002867 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbarf7b8eec2009-06-29 20:52:51 +00002868}
Ed Schoutenc66a5a32009-04-02 19:13:12 +00002869
Daniel Dunbar68a31d42009-03-31 17:45:15 +00002870void freebsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002871 Job &Dest, const InputInfo &Output,
2872 const InputInfoList &Inputs,
2873 const ArgList &Args,
Mike Stump1eb44332009-09-09 15:08:12 +00002874 const char *LinkingOutput) const {
Daniel Dunbar68a31d42009-03-31 17:45:15 +00002875 ArgStringList CmdArgs;
2876
Daniel Dunbar008f54a2009-04-01 19:36:32 +00002877 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
2878 // instruct as in the base system to assemble 32-bit code.
2879 if (getToolChain().getArchName() == "i386")
Daniel Dunbar68a31d42009-03-31 17:45:15 +00002880 CmdArgs.push_back("--32");
2881
Eric Christophered734732010-03-02 02:41:08 +00002882
2883 // Set byte order explicitly
2884 if (getToolChain().getArchName() == "mips")
2885 CmdArgs.push_back("-EB");
2886 else if (getToolChain().getArchName() == "mipsel")
2887 CmdArgs.push_back("-EL");
2888
Daniel Dunbar68a31d42009-03-31 17:45:15 +00002889 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
2890 options::OPT_Xassembler);
2891
2892 CmdArgs.push_back("-o");
2893 if (Output.isPipe())
2894 CmdArgs.push_back("-");
2895 else
2896 CmdArgs.push_back(Output.getFilename());
2897
2898 for (InputInfoList::const_iterator
2899 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2900 const InputInfo &II = *it;
2901 if (II.isPipe())
2902 CmdArgs.push_back("-");
2903 else
2904 CmdArgs.push_back(II.getFilename());
2905 }
2906
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002907 const char *Exec =
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00002908 Args.MakeArgString(getToolChain().GetProgramPath(C, "as"));
Daniel Dunbardaab7b12009-12-02 03:23:25 +00002909 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar68a31d42009-03-31 17:45:15 +00002910}
Daniel Dunbar008f54a2009-04-01 19:36:32 +00002911
2912void freebsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002913 Job &Dest, const InputInfo &Output,
2914 const InputInfoList &Inputs,
2915 const ArgList &Args,
Daniel Dunbara8304f62009-05-02 20:14:53 +00002916 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00002917 const Driver &D = getToolChain().getDriver();
Daniel Dunbar008f54a2009-04-01 19:36:32 +00002918 ArgStringList CmdArgs;
2919
2920 if (Args.hasArg(options::OPT_static)) {
2921 CmdArgs.push_back("-Bstatic");
2922 } else {
2923 CmdArgs.push_back("--eh-frame-hdr");
2924 if (Args.hasArg(options::OPT_shared)) {
2925 CmdArgs.push_back("-Bshareable");
2926 } else {
2927 CmdArgs.push_back("-dynamic-linker");
2928 CmdArgs.push_back("/libexec/ld-elf.so.1");
2929 }
2930 }
2931
2932 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
2933 // instruct ld in the base system to link 32-bit code.
2934 if (getToolChain().getArchName() == "i386") {
2935 CmdArgs.push_back("-m");
2936 CmdArgs.push_back("elf_i386_fbsd");
2937 }
2938
2939 if (Output.isPipe()) {
2940 CmdArgs.push_back("-o");
2941 CmdArgs.push_back("-");
2942 } else if (Output.isFilename()) {
2943 CmdArgs.push_back("-o");
2944 CmdArgs.push_back(Output.getFilename());
2945 } else {
2946 assert(Output.isNothing() && "Invalid output.");
2947 }
2948
2949 if (!Args.hasArg(options::OPT_nostdlib) &&
2950 !Args.hasArg(options::OPT_nostartfiles)) {
2951 if (!Args.hasArg(options::OPT_shared)) {
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00002952 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crt1.o")));
2953 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crti.o")));
2954 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtbegin.o")));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00002955 } else {
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00002956 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crti.o")));
2957 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtbeginS.o")));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00002958 }
2959 }
2960
2961 Args.AddAllArgs(CmdArgs, options::OPT_L);
2962 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
2963 Args.AddAllArgs(CmdArgs, options::OPT_e);
2964
2965 for (InputInfoList::const_iterator
Daniel Dunbarc21c4852009-04-08 23:54:23 +00002966 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
Daniel Dunbar008f54a2009-04-01 19:36:32 +00002967 const InputInfo &II = *it;
Daniel Dunbara8304f62009-05-02 20:14:53 +00002968
2969 // Don't try to pass LLVM inputs to a generic gcc.
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00002970 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
2971 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
Daniel Dunbara8304f62009-05-02 20:14:53 +00002972 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00002973 << getToolChain().getTripleString();
Daniel Dunbara8304f62009-05-02 20:14:53 +00002974
Daniel Dunbar008f54a2009-04-01 19:36:32 +00002975 if (II.isPipe())
2976 CmdArgs.push_back("-");
2977 else if (II.isFilename())
2978 CmdArgs.push_back(II.getFilename());
2979 else
2980 II.getInputArg().renderAsInput(Args, CmdArgs);
2981 }
2982
2983 if (!Args.hasArg(options::OPT_nostdlib) &&
2984 !Args.hasArg(options::OPT_nodefaultlibs)) {
Daniel Dunbar20022632010-02-17 08:07:51 +00002985 if (D.CCCIsCXX) {
2986 CmdArgs.push_back("-lstdc++");
2987 CmdArgs.push_back("-lm");
2988 }
Daniel Dunbar008f54a2009-04-01 19:36:32 +00002989 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
2990 // the default system libraries. Just mimic this for now.
2991 CmdArgs.push_back("-lgcc");
2992 if (Args.hasArg(options::OPT_static)) {
2993 CmdArgs.push_back("-lgcc_eh");
2994 } else {
2995 CmdArgs.push_back("--as-needed");
2996 CmdArgs.push_back("-lgcc_s");
2997 CmdArgs.push_back("--no-as-needed");
2998 }
2999
3000 if (Args.hasArg(options::OPT_pthread))
3001 CmdArgs.push_back("-lpthread");
3002 CmdArgs.push_back("-lc");
3003
3004 CmdArgs.push_back("-lgcc");
3005 if (Args.hasArg(options::OPT_static)) {
3006 CmdArgs.push_back("-lgcc_eh");
3007 } else {
3008 CmdArgs.push_back("--as-needed");
3009 CmdArgs.push_back("-lgcc_s");
3010 CmdArgs.push_back("--no-as-needed");
3011 }
3012 }
3013
3014 if (!Args.hasArg(options::OPT_nostdlib) &&
3015 !Args.hasArg(options::OPT_nostartfiles)) {
3016 if (!Args.hasArg(options::OPT_shared))
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00003017 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtend.o")));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003018 else
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00003019 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtendS.o")));
3020 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtn.o")));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003021 }
3022
Daniel Dunbarc21c4852009-04-08 23:54:23 +00003023 const char *Exec =
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00003024 Args.MakeArgString(getToolChain().GetProgramPath(C, "ld"));
Daniel Dunbardaab7b12009-12-02 03:23:25 +00003025 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar008f54a2009-04-01 19:36:32 +00003026}
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003027
3028/// DragonFly Tools
3029
3030// For now, DragonFly Assemble does just about the same as for
3031// FreeBSD, but this may change soon.
3032void dragonfly::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar294691e2009-11-04 06:24:38 +00003033 Job &Dest, const InputInfo &Output,
3034 const InputInfoList &Inputs,
3035 const ArgList &Args,
3036 const char *LinkingOutput) const {
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003037 ArgStringList CmdArgs;
3038
3039 // When building 32-bit code on DragonFly/pc64, we have to explicitly
3040 // instruct as in the base system to assemble 32-bit code.
3041 if (getToolChain().getArchName() == "i386")
3042 CmdArgs.push_back("--32");
3043
3044 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
3045 options::OPT_Xassembler);
3046
3047 CmdArgs.push_back("-o");
3048 if (Output.isPipe())
3049 CmdArgs.push_back("-");
3050 else
3051 CmdArgs.push_back(Output.getFilename());
3052
3053 for (InputInfoList::const_iterator
3054 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3055 const InputInfo &II = *it;
3056 if (II.isPipe())
3057 CmdArgs.push_back("-");
3058 else
3059 CmdArgs.push_back(II.getFilename());
3060 }
3061
3062 const char *Exec =
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00003063 Args.MakeArgString(getToolChain().GetProgramPath(C, "as"));
Daniel Dunbardaab7b12009-12-02 03:23:25 +00003064 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003065}
3066
3067void dragonfly::Link::ConstructJob(Compilation &C, const JobAction &JA,
3068 Job &Dest, const InputInfo &Output,
3069 const InputInfoList &Inputs,
3070 const ArgList &Args,
3071 const char *LinkingOutput) const {
Daniel Dunbaree788e72009-12-21 18:54:17 +00003072 const Driver &D = getToolChain().getDriver();
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003073 ArgStringList CmdArgs;
3074
3075 if (Args.hasArg(options::OPT_static)) {
3076 CmdArgs.push_back("-Bstatic");
3077 } else {
3078 if (Args.hasArg(options::OPT_shared))
3079 CmdArgs.push_back("-Bshareable");
3080 else {
3081 CmdArgs.push_back("-dynamic-linker");
3082 CmdArgs.push_back("/usr/libexec/ld-elf.so.2");
3083 }
3084 }
3085
3086 // When building 32-bit code on DragonFly/pc64, we have to explicitly
3087 // instruct ld in the base system to link 32-bit code.
3088 if (getToolChain().getArchName() == "i386") {
3089 CmdArgs.push_back("-m");
3090 CmdArgs.push_back("elf_i386");
3091 }
3092
3093 if (Output.isPipe()) {
3094 CmdArgs.push_back("-o");
3095 CmdArgs.push_back("-");
3096 } else if (Output.isFilename()) {
3097 CmdArgs.push_back("-o");
3098 CmdArgs.push_back(Output.getFilename());
3099 } else {
3100 assert(Output.isNothing() && "Invalid output.");
3101 }
3102
3103 if (!Args.hasArg(options::OPT_nostdlib) &&
3104 !Args.hasArg(options::OPT_nostartfiles)) {
3105 if (!Args.hasArg(options::OPT_shared)) {
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00003106 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crt1.o")));
3107 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crti.o")));
3108 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtbegin.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003109 } else {
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00003110 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crti.o")));
3111 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtbeginS.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003112 }
3113 }
3114
3115 Args.AddAllArgs(CmdArgs, options::OPT_L);
3116 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
3117 Args.AddAllArgs(CmdArgs, options::OPT_e);
3118
3119 for (InputInfoList::const_iterator
3120 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
3121 const InputInfo &II = *it;
Daniel Dunbara8304f62009-05-02 20:14:53 +00003122
3123 // Don't try to pass LLVM inputs to a generic gcc.
Daniel Dunbar6c6424b2010-06-07 23:28:45 +00003124 if (II.getType() == types::TY_LLVM_IR || II.getType() == types::TY_LTO_IR ||
3125 II.getType() == types::TY_LLVM_BC || II.getType() == types::TY_LTO_BC)
Daniel Dunbara8304f62009-05-02 20:14:53 +00003126 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
Daniel Dunbar88137642009-09-09 22:32:48 +00003127 << getToolChain().getTripleString();
Daniel Dunbara8304f62009-05-02 20:14:53 +00003128
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003129 if (II.isPipe())
3130 CmdArgs.push_back("-");
3131 else if (II.isFilename())
3132 CmdArgs.push_back(II.getFilename());
3133 else
3134 II.getInputArg().renderAsInput(Args, CmdArgs);
3135 }
3136
3137 if (!Args.hasArg(options::OPT_nostdlib) &&
3138 !Args.hasArg(options::OPT_nodefaultlibs)) {
3139 // FIXME: GCC passes on -lgcc, -lgcc_pic and a whole lot of
3140 // rpaths
3141 CmdArgs.push_back("-L/usr/lib/gcc41");
3142
3143 if (!Args.hasArg(options::OPT_static)) {
3144 CmdArgs.push_back("-rpath");
3145 CmdArgs.push_back("/usr/lib/gcc41");
3146
3147 CmdArgs.push_back("-rpath-link");
3148 CmdArgs.push_back("/usr/lib/gcc41");
3149
3150 CmdArgs.push_back("-rpath");
3151 CmdArgs.push_back("/usr/lib");
3152
3153 CmdArgs.push_back("-rpath-link");
3154 CmdArgs.push_back("/usr/lib");
3155 }
3156
3157 if (Args.hasArg(options::OPT_shared)) {
3158 CmdArgs.push_back("-lgcc_pic");
3159 } else {
3160 CmdArgs.push_back("-lgcc");
3161 }
3162
3163
3164 if (Args.hasArg(options::OPT_pthread))
Mike Stump4d63f8b2009-10-31 20:11:46 +00003165 CmdArgs.push_back("-lpthread");
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003166
3167 if (!Args.hasArg(options::OPT_nolibc)) {
3168 CmdArgs.push_back("-lc");
3169 }
3170
3171 if (Args.hasArg(options::OPT_shared)) {
3172 CmdArgs.push_back("-lgcc_pic");
3173 } else {
3174 CmdArgs.push_back("-lgcc");
3175 }
3176 }
3177
3178 if (!Args.hasArg(options::OPT_nostdlib) &&
3179 !Args.hasArg(options::OPT_nostartfiles)) {
3180 if (!Args.hasArg(options::OPT_shared))
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00003181 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtend.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003182 else
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00003183 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtendS.o")));
3184 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtn.o")));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003185 }
3186
3187 const char *Exec =
Daniel Dunbar5ed34f42009-09-09 22:33:00 +00003188 Args.MakeArgString(getToolChain().GetProgramPath(C, "ld"));
Daniel Dunbardaab7b12009-12-02 03:23:25 +00003189 Dest.addCommand(new Command(JA, *this, Exec, CmdArgs));
Daniel Dunbar11e1b402009-05-02 18:28:39 +00003190}