blob: 7000590654039a214bce3678e0fecaf1515be9b6 [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 Dunbar1d460332009-03-18 10:01:51 +000015#include "clang/Driver/Driver.h" // FIXME: Remove?
16#include "clang/Driver/DriverDiagnostic.h" // FIXME: Remove?
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"
21#include "clang/Driver/ToolChain.h"
Daniel Dunbar871adcf2009-03-18 07:06:02 +000022#include "clang/Driver/Util.h"
23
24#include "llvm/ADT/SmallVector.h"
25
26#include "InputInfo.h"
27
Daniel Dunbar47ac7d22009-03-18 06:00:36 +000028using namespace clang::driver;
29using namespace clang::driver::tools;
30
31void Clang::ConstructJob(Compilation &C, const JobAction &JA,
Daniel Dunbar871adcf2009-03-18 07:06:02 +000032 Job &Dest,
33 const InputInfo &Output,
Daniel Dunbar62cf6012009-03-18 06:07:59 +000034 const InputInfoList &Inputs,
Daniel Dunbar1d460332009-03-18 10:01:51 +000035 const ArgList &Args,
Daniel Dunbar47ac7d22009-03-18 06:00:36 +000036 const char *LinkingOutput) const {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +000037 ArgStringList CmdArgs;
38
Daniel Dunbar1d460332009-03-18 10:01:51 +000039 if (isa<AnalyzeJobAction>(JA)) {
40 assert(JA.getType() == types::TY_Plist && "Invalid output type.");
41 CmdArgs.push_back("-analyze");
42 } else if (isa<PreprocessJobAction>(JA)) {
43 CmdArgs.push_back("-E");
44 } else if (isa<PrecompileJobAction>(JA)) {
45 // No special option needed, driven by -x.
46 //
47 // FIXME: Don't drive this by -x, that is gross.
48
49 // FIXME: This is a total hack. Copy the input header file
50 // to the output, so that it can be -include'd by clang.
51 assert(Inputs.size() == 1 && "Cannot make PCH with multiple inputs.");
Daniel Dunbar115a7922009-03-19 07:29:38 +000052 assert(Output.isFilename() && "Unexpected output");
Daniel Dunbar1d460332009-03-18 10:01:51 +000053 assert(!Inputs[0].isPipe() && "Unexpected pipe");
Daniel Dunbar115a7922009-03-19 07:29:38 +000054 assert(Inputs[0].isFilename() && "Unexpected input");
55 const char *InputPath = Inputs[0].getFilename();
56 llvm::sys::Path OutputPath(Output.getFilename());
Daniel Dunbar1d460332009-03-18 10:01:51 +000057 OutputPath.eraseComponent();
58 if (OutputPath.empty())
59 OutputPath = llvm::sys::Path(InputPath).getLast();
Daniel Dunbarb488c1d2009-03-18 08:07:30 +000060 else
Daniel Dunbar1d460332009-03-18 10:01:51 +000061 OutputPath.appendComponent(llvm::sys::Path(InputPath).getLast());
62 if (!OutputPath.exists()) {
63 ArgStringList CpArgs;
64 CpArgs.push_back(InputPath);
65 CpArgs.push_back(Args.MakeArgString(OutputPath.c_str()));
Daniel Dunbarecc63622009-03-18 23:34:15 +000066 const char *Exec =
67 Args.MakeArgString(getToolChain().GetProgramPath(C, "cp").c_str());
68 C.getJobs().addJob(new Command(Exec, CpArgs));
Daniel Dunbar1d460332009-03-18 10:01:51 +000069 }
70 } else {
71 assert(isa<CompileJobAction>(JA) && "Invalid action for clang tool.");
72
73 if (JA.getType() == types::TY_Nothing) {
74 CmdArgs.push_back("-fsyntax-only");
75 } else if (JA.getType() == types::TY_LLVMAsm) {
76 CmdArgs.push_back("-emit-llvm");
77 } else if (JA.getType() == types::TY_LLVMBC) {
78 CmdArgs.push_back("-emit-llvm-bc");
79 } else if (JA.getType() == types::TY_PP_Asm) {
80 CmdArgs.push_back("-S");
81 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +000082 }
83
Daniel Dunbar1d460332009-03-18 10:01:51 +000084 // The make clang go fast button.
85 CmdArgs.push_back("-disable-free");
86
87 if (isa<AnalyzeJobAction>(JA)) {
88 // Add default argument set.
89 //
90 // FIXME: Move into clang?
91 CmdArgs.push_back("-warn-dead-stores");
92 CmdArgs.push_back("-checker-cfref");
93 CmdArgs.push_back("-warn-objc-methodsigs");
94 // Do not enable the missing -dealloc check.
95 // '-warn-objc-missing-dealloc',
96 CmdArgs.push_back("-warn-objc-unused-ivars");
97
98 CmdArgs.push_back("-analyzer-output=plist");
99
100 // Add -Xanalyzer arguments when running as analyzer.
101 Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
102 } else {
103 // Perform argument translation for LLVM backend. This
104 // takes some care in reconciling with llvm-gcc. The
105 // issue is that llvm-gcc translates these options based on
106 // the values in cc1, whereas we are processing based on
107 // the driver arguments.
108 //
109 // FIXME: This is currently broken for -f flags when -fno
110 // variants are present.
111
112 // This comes from the default translation the driver + cc1
113 // would do to enable flag_pic.
114 //
115 // FIXME: Centralize this code.
116 bool PICEnabled = (Args.hasArg(options::OPT_fPIC) ||
117 Args.hasArg(options::OPT_fpic) ||
118 Args.hasArg(options::OPT_fPIE) ||
119 Args.hasArg(options::OPT_fpie));
120 bool PICDisabled = (Args.hasArg(options::OPT_mkernel) ||
121 Args.hasArg(options::OPT_static));
122 const char *Model = getToolChain().GetForcedPicModel();
123 if (!Model) {
124 if (Args.hasArg(options::OPT_mdynamic_no_pic))
125 Model = "dynamic-no-pic";
126 else if (PICDisabled)
127 Model = "static";
128 else if (PICEnabled)
129 Model = "pic";
130 else
131 Model = getToolChain().GetDefaultRelocationModel();
132 }
133 CmdArgs.push_back("--relocation-model");
134 CmdArgs.push_back(Model);
135
136 if (Args.hasArg(options::OPT_ftime_report))
137 CmdArgs.push_back("--time-passes");
138 // FIXME: Set --enable-unsafe-fp-math.
139 if (!Args.hasArg(options::OPT_fomit_frame_pointer))
140 CmdArgs.push_back("--disable-fp-elim");
141 if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
142 options::OPT_fno_zero_initialized_in_bss,
143 true))
144 CmdArgs.push_back("--nozero-initialized-in-bss");
145 if (Args.hasArg(options::OPT_dA))
146 CmdArgs.push_back("--asm-verbose");
147 if (Args.hasArg(options::OPT_fdebug_pass_structure))
148 CmdArgs.push_back("--debug-pass=Structure");
149 if (Args.hasArg(options::OPT_fdebug_pass_arguments))
150 CmdArgs.push_back("--debug-pass=Arguments");
151 // FIXME: set --inline-threshhold=50 if (optimize_size || optimize
152 // < 3)
153 if (Args.hasFlag(options::OPT_funwind_tables,
154 options::OPT_fno_unwind_tables,
155 getToolChain().IsUnwindTablesDefault()))
156 CmdArgs.push_back("--unwind-tables=1");
157 else
158 CmdArgs.push_back("--unwind-tables=0");
159 if (!Args.hasFlag(options::OPT_mred_zone,
160 options::OPT_mno_red_zone,
161 true))
162 CmdArgs.push_back("--disable-red-zone");
163 if (Args.hasFlag(options::OPT_msoft_float,
164 options::OPT_mno_soft_float,
165 false))
166 CmdArgs.push_back("--soft-float");
167
168 // FIXME: Need target hooks.
169 if (memcmp(getToolChain().getPlatform().c_str(), "darwin", 6) == 0) {
170 if (getToolChain().getArchName() == "x86_64")
171 CmdArgs.push_back("--mcpu=core2");
172 else if (getToolChain().getArchName() == "i386")
173 CmdArgs.push_back("--mcpu=yonah");
174 }
175
176 // FIXME: Ignores ordering. Also, we need to find a realistic
177 // solution for this.
178 static const struct {
179 options::ID Pos, Neg;
180 const char *Name;
181 } FeatureOptions[] = {
182 { options::OPT_mmmx, options::OPT_mno_mmx, "mmx" },
183 { options::OPT_msse, options::OPT_mno_sse, "sse" },
184 { options::OPT_msse2, options::OPT_mno_sse2, "sse2" },
185 { options::OPT_msse3, options::OPT_mno_sse3, "sse3" },
186 { options::OPT_mssse3, options::OPT_mno_ssse3, "ssse3" },
187 { options::OPT_msse41, options::OPT_mno_sse41, "sse41" },
188 { options::OPT_msse42, options::OPT_mno_sse42, "sse42" },
189 { options::OPT_msse4a, options::OPT_mno_sse4a, "sse4a" },
190 { options::OPT_m3dnow, options::OPT_mno_3dnow, "3dnow" },
191 { options::OPT_m3dnowa, options::OPT_mno_3dnowa, "3dnowa" }
192 };
193 const unsigned NumFeatureOptions =
194 sizeof(FeatureOptions)/sizeof(FeatureOptions[0]);
195
196 // FIXME: Avoid std::string
197 std::string Attrs;
198 for (unsigned i=0; i < NumFeatureOptions; ++i) {
199 if (Args.hasArg(FeatureOptions[i].Pos)) {
200 Attrs += '+';
201 Attrs += FeatureOptions[i].Name;
202 } else if (Args.hasArg(FeatureOptions[i].Neg)) {
203 Attrs += '-';
204 Attrs += FeatureOptions[i].Name;
205 }
206 }
207 if (!Attrs.empty()) {
208 CmdArgs.push_back("--mattr");
209 CmdArgs.push_back(Args.MakeArgString(Attrs.c_str()));
210 }
211
212 if (Args.hasFlag(options::OPT_fmath_errno,
213 options::OPT_fno_math_errno,
214 getToolChain().IsMathErrnoDefault()))
215 CmdArgs.push_back("--fmath-errno=1");
216 else
217 CmdArgs.push_back("--fmath-errno=0");
218
219 if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
220 CmdArgs.push_back("--limit-float-precision");
221 CmdArgs.push_back(A->getValue(Args));
222 }
223
224 // FIXME: Add --stack-protector-buffer-size=<xxx> on
225 // -fstack-protect.
226
227 Args.AddLastArg(CmdArgs, options::OPT_MD);
228 Args.AddLastArg(CmdArgs, options::OPT_MM);
229 Args.AddAllArgs(CmdArgs, options::OPT_MF);
230 Args.AddLastArg(CmdArgs, options::OPT_MP);
231 Args.AddAllArgs(CmdArgs, options::OPT_MT);
232
233 Arg *Unsupported = Args.getLastArg(options::OPT_M);
234 if (!Unsupported)
235 Unsupported = Args.getLastArg(options::OPT_MM);
236 if (!Unsupported)
237 Unsupported = Args.getLastArg(options::OPT_MG);
238 if (!Unsupported)
239 Unsupported = Args.getLastArg(options::OPT_MQ);
240 if (Unsupported) {
241 const Driver &D = getToolChain().getHost().getDriver();
242 D.Diag(clang::diag::err_drv_unsupported_opt)
243 << Unsupported->getOption().getName();
244 }
245 }
246
247 Args.AddAllArgs(CmdArgs, options::OPT_v);
248 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U);
249 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F);
250 Args.AddLastArg(CmdArgs, options::OPT_P);
251 Args.AddAllArgs(CmdArgs, options::OPT_mmacosx_version_min_EQ);
252
253 // Special case debug options to only pass -g to clang. This is
254 // wrong.
255 if (Args.hasArg(options::OPT_g_Group))
256 CmdArgs.push_back("-g");
257
258 Args.AddLastArg(CmdArgs, options::OPT_nostdinc);
259
260 // FIXME: Clang isn't going to accept just anything here.
261 Args.AddAllArgs(CmdArgs, options::OPT_i_Group);
262
263 // Automatically load .pth or .gch files which match -include
264 // options. It's wonky, but we include looking for .gch so we can
265 // support seamless replacement into a build system already set up
266 // to be generating .gch files.
267
268 // FIXME: Need to use an iterator for this to be efficient.
269 for (ArgList::const_iterator
270 it = Args.begin(), ie = Args.end(); it != ie; ++it) {
271 const Arg *A = *it;
272 if (A->getOption().matches(options::OPT_include)) {
273 llvm::sys::Path P(A->getValue(Args));
274 P.appendSuffix("pth");
275 if (P.exists()) {
276 CmdArgs.push_back("-token-cache");
277 CmdArgs.push_back(Args.MakeArgString(P.c_str()));
278 } else {
279 P.eraseSuffix();
280 P.appendSuffix("gch");
281 if (P.exists()) {
282 CmdArgs.push_back("-token-cache");
283 CmdArgs.push_back(Args.MakeArgString(P.c_str()));
284 }
285 }
286 }
287 }
288
289 // Manually translate -O to -O1; let clang reject others.
290 if (Arg *A = Args.getLastArg(options::OPT_O)) {
291 if (A->getValue(Args)[0] == '\0')
292 CmdArgs.push_back("-O1");
293 else
Daniel Dunbar5697aa02009-03-18 23:39:35 +0000294 A->render(Args, CmdArgs);
Daniel Dunbar1d460332009-03-18 10:01:51 +0000295 }
296
297 Args.AddAllArgs(CmdArgs, options::OPT_clang_W_Group, options::OPT_pedantic_Group);
298 Args.AddLastArg(CmdArgs, options::OPT_w);
299 Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
300 options::OPT_trigraphs);
301
302 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_)) {
303 CmdArgs.push_back("-ftemplate-depth");
304 CmdArgs.push_back(A->getValue(Args));
305 }
306
307 Args.AddAllArgs(CmdArgs, options::OPT_clang_f_Group);
308
309 Args.AddLastArg(CmdArgs, options::OPT_dM);
310
311 Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
312
313 // FIXME: Always pass the full triple once we aren't concerned with
314 // ccc compat.
315 CmdArgs.push_back("-arch");
316 CmdArgs.push_back(getToolChain().getArchName().c_str());
317
Daniel Dunbarb488c1d2009-03-18 08:07:30 +0000318 if (Output.isPipe()) {
319 CmdArgs.push_back("-o");
320 CmdArgs.push_back("-");
Daniel Dunbar115a7922009-03-19 07:29:38 +0000321 } else if (Output.isFilename()) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +0000322 CmdArgs.push_back("-o");
Daniel Dunbar115a7922009-03-19 07:29:38 +0000323 CmdArgs.push_back(Output.getFilename());
324 } else {
325 assert(Output.isNothing() && "Invalid output.");
Daniel Dunbarb488c1d2009-03-18 08:07:30 +0000326 }
327
Daniel Dunbar1d460332009-03-18 10:01:51 +0000328 for (InputInfoList::const_iterator
329 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
330 const InputInfo &II = *it;
331 CmdArgs.push_back("-x");
332 CmdArgs.push_back(types::getTypeName(II.getType()));
333 if (II.isPipe())
334 CmdArgs.push_back("-");
Daniel Dunbar115a7922009-03-19 07:29:38 +0000335 else if (II.isFilename())
336 CmdArgs.push_back(II.getFilename());
Daniel Dunbar1d460332009-03-18 10:01:51 +0000337 else
Daniel Dunbar115a7922009-03-19 07:29:38 +0000338 II.getInputArg().renderAsInput(Args, CmdArgs);
Daniel Dunbar1d460332009-03-18 10:01:51 +0000339 }
340
341 const char *Exec =
342 Args.MakeArgString(getToolChain().GetProgramPath(C, "clang").c_str());
343 Dest.addCommand(new Command(Exec, CmdArgs));
Daniel Dunbar47ac7d22009-03-18 06:00:36 +0000344}
345
Daniel Dunbarb488c1d2009-03-18 08:07:30 +0000346void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA,
347 Job &Dest,
348 const InputInfo &Output,
349 const InputInfoList &Inputs,
Daniel Dunbar1d460332009-03-18 10:01:51 +0000350 const ArgList &Args,
Daniel Dunbarb488c1d2009-03-18 08:07:30 +0000351 const char *LinkingOutput) const {
352 ArgStringList CmdArgs;
Daniel Dunbar47ac7d22009-03-18 06:00:36 +0000353
Daniel Dunbarb488c1d2009-03-18 08:07:30 +0000354 for (ArgList::const_iterator
Daniel Dunbar1d460332009-03-18 10:01:51 +0000355 it = Args.begin(), ie = Args.end(); it != ie; ++it) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +0000356 Arg *A = *it;
Daniel Dunbar75877192009-03-19 07:55:12 +0000357 if (A->getOption().hasForwardToGCC()) {
358 // It is unfortunate that we have to claim here, as this means
359 // we will basically never report anything interesting for
360 // platforms using a generic gcc.
361 A->claim();
Daniel Dunbar1d460332009-03-18 10:01:51 +0000362 A->render(Args, CmdArgs);
Daniel Dunbar75877192009-03-19 07:55:12 +0000363 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +0000364 }
365
366 RenderExtraToolArgs(CmdArgs);
367
368 // If using a driver driver, force the arch.
369 if (getToolChain().getHost().useDriverDriver()) {
370 CmdArgs.push_back("-arch");
371 CmdArgs.push_back(getToolChain().getArchName().c_str());
372 }
373
374 if (Output.isPipe()) {
375 CmdArgs.push_back("-o");
376 CmdArgs.push_back("-");
Daniel Dunbar115a7922009-03-19 07:29:38 +0000377 } else if (Output.isFilename()) {
Daniel Dunbarb488c1d2009-03-18 08:07:30 +0000378 CmdArgs.push_back("-o");
Daniel Dunbar115a7922009-03-19 07:29:38 +0000379 CmdArgs.push_back(Output.getFilename());
380 } else {
381 assert(Output.isNothing() && "Unexpected output");
Daniel Dunbarb488c1d2009-03-18 08:07:30 +0000382 CmdArgs.push_back("-fsyntax-only");
Daniel Dunbar115a7922009-03-19 07:29:38 +0000383 }
Daniel Dunbarb488c1d2009-03-18 08:07:30 +0000384
385
386 // Only pass -x if gcc will understand it; otherwise hope gcc
387 // understands the suffix correctly. The main use case this would go
388 // wrong in is for linker inputs if they happened to have an odd
389 // suffix; really the only way to get this to happen is a command
390 // like '-x foobar a.c' which will treat a.c like a linker input.
391 //
392 // FIXME: For the linker case specifically, can we safely convert
393 // inputs into '-Wl,' options?
394 for (InputInfoList::const_iterator
395 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
396 const InputInfo &II = *it;
397 if (types::canTypeBeUserSpecified(II.getType())) {
398 CmdArgs.push_back("-x");
399 CmdArgs.push_back(types::getTypeName(II.getType()));
400 }
401
402 if (II.isPipe())
403 CmdArgs.push_back("-");
Daniel Dunbar115a7922009-03-19 07:29:38 +0000404 else if (II.isFilename())
405 CmdArgs.push_back(II.getFilename());
Daniel Dunbarb488c1d2009-03-18 08:07:30 +0000406 else
Daniel Dunbar115a7922009-03-19 07:29:38 +0000407 // Don't render as input, we need gcc to do the translations.
408 II.getInputArg().render(Args, CmdArgs);
Daniel Dunbarb488c1d2009-03-18 08:07:30 +0000409 }
410
Daniel Dunbar632f50e2009-03-18 21:34:08 +0000411 const char *Exec =
412 Args.MakeArgString(getToolChain().GetProgramPath(C, "gcc").c_str());
413 Dest.addCommand(new Command(Exec, CmdArgs));
Daniel Dunbar47ac7d22009-03-18 06:00:36 +0000414}
415
Daniel Dunbarb488c1d2009-03-18 08:07:30 +0000416void gcc::Preprocess::RenderExtraToolArgs(ArgStringList &CmdArgs) const {
417 CmdArgs.push_back("-E");
Daniel Dunbar47ac7d22009-03-18 06:00:36 +0000418}
419
Daniel Dunbarb488c1d2009-03-18 08:07:30 +0000420void gcc::Precompile::RenderExtraToolArgs(ArgStringList &CmdArgs) const {
421 // The type is good enough.
Daniel Dunbar47ac7d22009-03-18 06:00:36 +0000422}
423
Daniel Dunbarb488c1d2009-03-18 08:07:30 +0000424void gcc::Compile::RenderExtraToolArgs(ArgStringList &CmdArgs) const {
425 CmdArgs.push_back("-S");
Daniel Dunbar47ac7d22009-03-18 06:00:36 +0000426}
427
Daniel Dunbarb488c1d2009-03-18 08:07:30 +0000428void gcc::Assemble::RenderExtraToolArgs(ArgStringList &CmdArgs) const {
429 CmdArgs.push_back("-c");
Daniel Dunbar47ac7d22009-03-18 06:00:36 +0000430}
Daniel Dunbarb488c1d2009-03-18 08:07:30 +0000431
432void gcc::Link::RenderExtraToolArgs(ArgStringList &CmdArgs) const {
433 // The types are (hopefully) good enough.
434}
435