1 //===--- Tools.cpp - Tools Implementations ------------------------------*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #include "clang/Driver/Action.h"
13 #include "clang/Driver/Arg.h"
14 #include "clang/Driver/ArgList.h"
15 #include "clang/Driver/Driver.h" // FIXME: Remove?
16 #include "clang/Driver/DriverDiagnostic.h" // FIXME: Remove?
17 #include "clang/Driver/Compilation.h"
18 #include "clang/Driver/Job.h"
19 #include "clang/Driver/HostInfo.h"
20 #include "clang/Driver/Option.h"
21 #include "clang/Driver/ToolChain.h"
22 #include "clang/Driver/Util.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/Support/Format.h"
26 #include "llvm/Support/raw_ostream.h"
28 #include "InputInfo.h"
29 #include "ToolChains.h"
31 using namespace clang::driver;
32 using namespace clang::driver::tools;
34 static const char *MakeFormattedString(const ArgList &Args,
35 const llvm::format_object_base &Fmt) {
37 llvm::raw_string_ostream(Str) << Fmt;
38 return Args.MakeArgString(Str.c_str());
41 void Clang::AddPreprocessingOptions(const Driver &D,
43 ArgStringList &CmdArgs,
44 const InputInfo &Output,
45 const InputInfoList &Inputs) const {
48 if ((A = Args.getLastArg(options::OPT_C)) ||
49 (A = Args.getLastArg(options::OPT_CC))) {
50 if (!Args.hasArg(options::OPT_E))
51 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
52 << A->getAsString(Args) << "-E";
53 A->render(Args, CmdArgs);
56 // Handle dependency file generation.
57 if ((A = Args.getLastArg(options::OPT_M)) ||
58 (A = Args.getLastArg(options::OPT_MM)) ||
59 (A = Args.getLastArg(options::OPT_MD)) ||
60 (A = Args.getLastArg(options::OPT_MMD))) {
61 // Determine the output location.
63 if (Output.getType() == types::TY_Dependencies) {
67 DepFile = Output.getFilename();
68 } else if (Arg *MF = Args.getLastArg(options::OPT_MF)) {
69 DepFile = MF->getValue(Args);
70 } else if (A->getOption().getId() == options::OPT_M ||
71 A->getOption().getId() == options::OPT_MM) {
74 DepFile = darwin::CC1::getDependencyFileName(Args, Inputs);
76 CmdArgs.push_back("-dependency-file");
77 CmdArgs.push_back(DepFile);
79 // Add an -MT option if the user didn't specify their own.
80 // FIXME: This should use -MQ, when we support it.
81 if (!Args.hasArg(options::OPT_MT) && !Args.hasArg(options::OPT_MQ)) {
82 const char *DepTarget;
84 // If user provided -o, that is the dependency target, except
85 // when we are only generating a dependency file.
86 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
87 if (OutputOpt && Output.getType() != types::TY_Dependencies) {
88 DepTarget = OutputOpt->getValue(Args);
90 // Otherwise derive from the base input.
92 // FIXME: This should use the computed output file location.
93 llvm::sys::Path P(Inputs[0].getBaseInput());
97 DepTarget = Args.MakeArgString(P.getLast().c_str());
100 CmdArgs.push_back("-MT");
101 CmdArgs.push_back(DepTarget);
104 if (A->getOption().getId() == options::OPT_M ||
105 A->getOption().getId() == options::OPT_MD)
106 CmdArgs.push_back("-sys-header-deps");
109 Args.AddLastArg(CmdArgs, options::OPT_MP);
110 Args.AddAllArgs(CmdArgs, options::OPT_MT);
112 // FIXME: Use iterator.
114 // Add -i* options, and automatically translate to
115 // -include-pch/-include-pth for transparent PCH support. It's
116 // wonky, but we include looking for .gch so we can support seamless
117 // replacement into a build system already set up to be generating
119 for (ArgList::const_iterator
120 it = Args.begin(), ie = Args.end(); it != ie; ++it) {
122 if (!A->getOption().matches(options::OPT_clang_i_Group))
125 if (A->getOption().matches(options::OPT_include)) {
126 bool FoundPTH = false;
127 bool FoundPCH = false;
128 llvm::sys::Path P(A->getValue(Args));
130 P.appendSuffix("pch");
138 P.appendSuffix("pth");
145 if (!FoundPCH && !FoundPTH) {
146 P.appendSuffix("gch");
148 FoundPCH = D.CCCUsePCH;
149 FoundPTH = !D.CCCUsePCH;
155 if (FoundPCH || FoundPTH) {
158 CmdArgs.push_back("-include-pch");
160 CmdArgs.push_back("-include-pth");
161 CmdArgs.push_back(Args.MakeArgString(P.c_str()));
166 // Not translated, render as usual.
168 A->render(Args, CmdArgs);
171 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U);
172 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F);
174 // Add -Wp, and -Xassembler if using the preprocessor.
176 // FIXME: There is a very unfortunate problem here, some troubled
177 // souls abuse -Wp, to pass preprocessor options in gcc syntax. To
178 // really support that we would have to parse and then translate
180 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
181 options::OPT_Xpreprocessor);
184 void Clang::ConstructJob(Compilation &C, const JobAction &JA,
186 const InputInfo &Output,
187 const InputInfoList &Inputs,
189 const char *LinkingOutput) const {
190 const Driver &D = getToolChain().getHost().getDriver();
191 ArgStringList CmdArgs;
193 assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
195 CmdArgs.push_back("-triple");
196 const char *TripleStr =
197 Args.MakeArgString(getToolChain().getTripleString().c_str());
198 CmdArgs.push_back(TripleStr);
200 if (isa<AnalyzeJobAction>(JA)) {
201 assert(JA.getType() == types::TY_Plist && "Invalid output type.");
202 CmdArgs.push_back("-analyze");
203 } else if (isa<PreprocessJobAction>(JA)) {
204 if (Output.getType() == types::TY_Dependencies)
205 CmdArgs.push_back("-Eonly");
207 CmdArgs.push_back("-E");
208 } else if (isa<PrecompileJobAction>(JA)) {
210 CmdArgs.push_back("-emit-pch");
212 CmdArgs.push_back("-emit-pth");
214 assert(isa<CompileJobAction>(JA) && "Invalid action for clang tool.");
216 if (JA.getType() == types::TY_Nothing) {
217 CmdArgs.push_back("-fsyntax-only");
218 } else if (JA.getType() == types::TY_LLVMAsm) {
219 CmdArgs.push_back("-emit-llvm");
220 } else if (JA.getType() == types::TY_LLVMBC) {
221 CmdArgs.push_back("-emit-llvm-bc");
222 } else if (JA.getType() == types::TY_PP_Asm) {
223 CmdArgs.push_back("-S");
227 // The make clang go fast button.
228 CmdArgs.push_back("-disable-free");
230 // Set the main file name, so that debug info works even with
232 CmdArgs.push_back("-main-file-name");
233 CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
235 // Some flags which affect the language (via preprocessor
236 // defines). See darwin::CC1::AddCPPArgs.
237 if (Args.hasArg(options::OPT_static))
238 CmdArgs.push_back("-static-define");
240 if (isa<AnalyzeJobAction>(JA)) {
241 // Add default argument set.
242 if (!Args.hasArg(options::OPT__analyzer_no_default_checks)) {
243 CmdArgs.push_back("-warn-dead-stores");
244 CmdArgs.push_back("-checker-cfref");
245 CmdArgs.push_back("-analyzer-eagerly-assume");
246 CmdArgs.push_back("-warn-objc-methodsigs");
247 // Do not enable the missing -dealloc check.
248 // '-warn-objc-missing-dealloc',
249 CmdArgs.push_back("-warn-objc-unused-ivars");
252 // Set the output format. The default is plist, for (lame) historical
254 CmdArgs.push_back("-analyzer-output");
255 if (Arg *A = Args.getLastArg(options::OPT__analyzer_output))
256 CmdArgs.push_back(A->getValue(Args));
258 CmdArgs.push_back("plist");
260 // Add -Xanalyzer arguments when running as analyzer.
261 Args.AddAllArgValues(CmdArgs, options::OPT_Xanalyzer);
264 // Perform argument translation for LLVM backend. This
265 // takes some care in reconciling with llvm-gcc. The
266 // issue is that llvm-gcc translates these options based on
267 // the values in cc1, whereas we are processing based on
268 // the driver arguments.
270 // FIXME: This is currently broken for -f flags when -fno
271 // variants are present.
273 // This comes from the default translation the driver + cc1
274 // would do to enable flag_pic.
276 // FIXME: Centralize this code.
277 bool PICEnabled = (Args.hasArg(options::OPT_fPIC) ||
278 Args.hasArg(options::OPT_fpic) ||
279 Args.hasArg(options::OPT_fPIE) ||
280 Args.hasArg(options::OPT_fpie));
281 bool PICDisabled = (Args.hasArg(options::OPT_mkernel) ||
282 Args.hasArg(options::OPT_static));
283 const char *Model = getToolChain().GetForcedPicModel();
285 if (Args.hasArg(options::OPT_mdynamic_no_pic))
286 Model = "dynamic-no-pic";
287 else if (PICDisabled)
292 Model = getToolChain().GetDefaultRelocationModel();
294 CmdArgs.push_back("--relocation-model");
295 CmdArgs.push_back(Model);
297 // Infer the __PIC__ value.
299 // FIXME: This isn't quite right on Darwin, which always sets
301 if (strcmp(Model, "pic") == 0 || strcmp(Model, "dynamic-no-pic") == 0) {
302 if (Args.hasArg(options::OPT_fPIC))
303 CmdArgs.push_back("-pic-level=2");
305 CmdArgs.push_back("-pic-level=1");
308 if (Args.hasArg(options::OPT_ftime_report))
309 CmdArgs.push_back("--time-passes");
310 // FIXME: Set --enable-unsafe-fp-math.
311 if (!Args.hasArg(options::OPT_fomit_frame_pointer))
312 CmdArgs.push_back("--disable-fp-elim");
313 if (!Args.hasFlag(options::OPT_fzero_initialized_in_bss,
314 options::OPT_fno_zero_initialized_in_bss,
316 CmdArgs.push_back("--nozero-initialized-in-bss");
317 if (Args.hasArg(options::OPT_dA) || Args.hasArg(options::OPT_fverbose_asm))
318 CmdArgs.push_back("--asm-verbose");
319 if (Args.hasArg(options::OPT_fdebug_pass_structure))
320 CmdArgs.push_back("--debug-pass=Structure");
321 if (Args.hasArg(options::OPT_fdebug_pass_arguments))
322 CmdArgs.push_back("--debug-pass=Arguments");
323 // FIXME: set --inline-threshhold=50 if (optimize_size || optimize
325 if (Args.hasFlag(options::OPT_funwind_tables,
326 options::OPT_fno_unwind_tables,
327 (getToolChain().IsUnwindTablesDefault() &&
328 !Args.hasArg(options::OPT_mkernel))))
329 CmdArgs.push_back("--unwind-tables=1");
331 CmdArgs.push_back("--unwind-tables=0");
332 if (!Args.hasFlag(options::OPT_mred_zone,
333 options::OPT_mno_red_zone,
335 Args.hasArg(options::OPT_mkernel) ||
336 Args.hasArg(options::OPT_fapple_kext))
337 CmdArgs.push_back("--disable-red-zone");
338 if (Args.hasFlag(options::OPT_msoft_float,
339 options::OPT_mno_soft_float,
341 CmdArgs.push_back("--no-implicit-float");
343 // FIXME: Handle -mtune=.
344 (void) Args.hasArg(options::OPT_mtune_EQ);
346 if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) {
347 // FIXME: We may need some translation here from the options gcc takes to
348 // names the LLVM backend understand?
349 CmdArgs.push_back("-mcpu");
350 CmdArgs.push_back(A->getValue(Args));
352 // Select default CPU.
354 // FIXME: Need target hooks.
355 if (memcmp(getToolChain().getOS().c_str(), "darwin", 6) == 0) {
356 if (getToolChain().getArchName() == "x86_64")
357 CmdArgs.push_back("--mcpu=core2");
358 else if (getToolChain().getArchName() == "i386")
359 CmdArgs.push_back("--mcpu=yonah");
361 if (getToolChain().getArchName() == "x86_64")
362 CmdArgs.push_back("--mcpu=x86-64");
363 else if (getToolChain().getArchName() == "i386")
364 CmdArgs.push_back("--mcpu=pentium4");
368 // FIXME: Use iterator.
369 for (ArgList::const_iterator
370 it = Args.begin(), ie = Args.end(); it != ie; ++it) {
372 if (A->getOption().matches(options::OPT_m_x86_Features_Group)) {
373 const char *Name = A->getOption().getName();
376 assert(Name[0] == '-' && Name[1] == 'm' && "Invalid feature name.");
379 bool IsNegative = memcmp(Name, "no-", 3) == 0;
384 CmdArgs.push_back("-target-feature");
385 CmdArgs.push_back(MakeFormattedString(Args,
387 IsNegative ? '-' : '+',
392 if (Args.hasFlag(options::OPT_fmath_errno,
393 options::OPT_fno_math_errno,
394 getToolChain().IsMathErrnoDefault()))
395 CmdArgs.push_back("--fmath-errno=1");
397 CmdArgs.push_back("--fmath-errno=0");
399 if (Arg *A = Args.getLastArg(options::OPT_flimited_precision_EQ)) {
400 CmdArgs.push_back("--limit-float-precision");
401 CmdArgs.push_back(A->getValue(Args));
404 // FIXME: Add --stack-protector-buffer-size=<xxx> on
408 if ((Unsupported = Args.getLastArg(options::OPT_MG)) ||
409 (Unsupported = Args.getLastArg(options::OPT_MQ)) ||
410 (Unsupported = Args.getLastArg(options::OPT_iframework)))
411 D.Diag(clang::diag::err_drv_clang_unsupported)
412 << Unsupported->getOption().getName();
414 Args.AddAllArgs(CmdArgs, options::OPT_v);
415 Args.AddLastArg(CmdArgs, options::OPT_P);
416 Args.AddLastArg(CmdArgs, options::OPT_mmacosx_version_min_EQ);
417 Args.AddLastArg(CmdArgs, options::OPT_miphoneos_version_min_EQ);
418 Args.AddLastArg(CmdArgs, options::OPT_print_ivar_layout);
420 // Special case debug options to only pass -g to clang. This is
422 if (Args.hasArg(options::OPT_g_Group))
423 CmdArgs.push_back("-g");
425 Args.AddLastArg(CmdArgs, options::OPT_nostdinc);
427 Args.AddLastArg(CmdArgs, options::OPT_isysroot);
429 // Add preprocessing options like -I, -D, etc. if we are using the
432 // FIXME: Support -fpreprocessed
433 types::ID InputType = Inputs[0].getType();
434 if (types::getPreprocessedType(InputType) != types::TY_INVALID)
435 AddPreprocessingOptions(D, Args, CmdArgs, Output, Inputs);
437 // Manually translate -O to -O1 and -O4 to -O3; let clang reject
439 if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
440 if (A->getOption().getId() == options::OPT_O4)
441 CmdArgs.push_back("-O3");
442 else if (A->getValue(Args)[0] == '\0')
443 CmdArgs.push_back("-O1");
445 A->render(Args, CmdArgs);
448 Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
449 Args.AddLastArg(CmdArgs, options::OPT_w);
451 // Handle -{std, ansi, trigraphs} -- take the last of -{std, ansi}
452 // (-ansi is equivalent to -std=c89).
454 // If a std is supplied, only add -trigraphs if it follows the
456 if (Arg *Std = Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
457 if (Std->getOption().matches(options::OPT_ansi))
458 CmdArgs.push_back("-std=c89");
460 Std->render(Args, CmdArgs);
462 if (Arg *A = Args.getLastArg(options::OPT_trigraphs))
463 if (A->getIndex() > Std->getIndex())
464 A->render(Args, CmdArgs);
466 // Honor -std-default.
467 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
468 "-std=", /*Joined=*/true);
469 Args.AddLastArg(CmdArgs, options::OPT_trigraphs);
472 if (Arg *A = Args.getLastArg(options::OPT_ftemplate_depth_)) {
473 CmdArgs.push_back("-ftemplate-depth");
474 CmdArgs.push_back(A->getValue(Args));
477 // Forward -f options which we can pass directly.
478 Args.AddLastArg(CmdArgs, options::OPT_femit_all_decls);
479 Args.AddLastArg(CmdArgs, options::OPT_fexceptions);
480 Args.AddLastArg(CmdArgs, options::OPT_ffreestanding);
481 Args.AddLastArg(CmdArgs, options::OPT_fheinous_gnu_extensions);
482 Args.AddLastArg(CmdArgs, options::OPT_fgnu_runtime);
483 Args.AddLastArg(CmdArgs, options::OPT_flax_vector_conversions);
484 Args.AddLastArg(CmdArgs, options::OPT_fmessage_length_EQ);
485 Args.AddLastArg(CmdArgs, options::OPT_fms_extensions);
486 Args.AddLastArg(CmdArgs, options::OPT_fnext_runtime);
487 Args.AddLastArg(CmdArgs, options::OPT_fno_caret_diagnostics);
488 Args.AddLastArg(CmdArgs, options::OPT_fno_show_column);
489 Args.AddLastArg(CmdArgs, options::OPT_fobjc_gc_only);
490 Args.AddLastArg(CmdArgs, options::OPT_fobjc_gc);
491 Args.AddLastArg(CmdArgs, options::OPT_fobjc_sender_dependent_dispatch);
492 // FIXME: Should we remove this?
493 Args.AddLastArg(CmdArgs, options::OPT_fobjc_nonfragile_abi);
494 Args.AddLastArg(CmdArgs, options::OPT_fobjc_tight_layout);
495 Args.AddLastArg(CmdArgs, options::OPT_fdiagnostics_print_source_range_info);
496 Args.AddLastArg(CmdArgs, options::OPT_ftime_report);
497 Args.AddLastArg(CmdArgs, options::OPT_ftrapv);
498 Args.AddLastArg(CmdArgs, options::OPT_fvisibility_EQ);
499 Args.AddLastArg(CmdArgs, options::OPT_fwritable_strings);
501 // Forward stack protector flags.
502 if (Arg *A = Args.getLastArg(options::OPT_fno_stack_protector,
503 options::OPT_fstack_protector_all,
504 options::OPT_fstack_protector)) {
505 if (A->getOption().matches(options::OPT_fno_stack_protector))
506 CmdArgs.push_back("--stack-protector=0");
507 else if (A->getOption().matches(options::OPT_fstack_protector))
508 CmdArgs.push_back("--stack-protector=1");
510 CmdArgs.push_back("--stack-protector=2");
513 // Forward -f options with positive and negative forms; we translate
516 // -fbuiltin is default, only pass non-default.
517 if (!Args.hasFlag(options::OPT_fbuiltin, options::OPT_fno_builtin))
518 CmdArgs.push_back("-fbuiltin=0");
520 // -fblocks default varies depending on platform and language; only
521 // pass if specified.
522 if (Arg *A = Args.getLastArg(options::OPT_fblocks, options::OPT_fno_blocks)) {
523 if (A->getOption().matches(options::OPT_fblocks))
524 CmdArgs.push_back("-fblocks");
526 CmdArgs.push_back("-fblocks=0");
529 // -fsigned-char/-funsigned-char default varies depending on platform; only
530 // pass if specified.
531 if (Arg *A = Args.getLastArg(options::OPT_fsigned_char,
532 options::OPT_funsigned_char)) {
533 if (A->getOption().matches(options::OPT_fsigned_char))
534 CmdArgs.push_back("-fsigned-char");
536 CmdArgs.push_back("-fsigned-char=0");
539 // -fno-pascal-strings is default, only pass non-default. If the
540 // -tool chain happened to translate to -mpascal-strings, we want to
541 // -back translate here.
543 // FIXME: This is gross; that translation should be pulled from the
545 if (Args.hasFlag(options::OPT_fpascal_strings,
546 options::OPT_fno_pascal_strings,
548 Args.hasFlag(options::OPT_mpascal_strings,
549 options::OPT_mno_pascal_strings,
551 CmdArgs.push_back("-fpascal-strings");
553 // -fcommon is default, only pass non-default.
554 if (!Args.hasFlag(options::OPT_fcommon, options::OPT_fno_common))
555 CmdArgs.push_back("-fno-common");
557 // -fsigned-bitfields is default, and clang doesn't yet support
558 // --funsigned-bitfields.
559 if (!Args.hasFlag(options::OPT_fsigned_bitfields,
560 options::OPT_funsigned_bitfields))
561 D.Diag(clang::diag::warn_drv_clang_unsupported)
562 << Args.getLastArg(options::OPT_funsigned_bitfields)->getAsString(Args);
564 // -fdiagnostics-fixit-info is default, only pass non-default.
565 if (!Args.hasFlag(options::OPT_fdiagnostics_fixit_info,
566 options::OPT_fno_diagnostics_fixit_info))
567 CmdArgs.push_back("-fno-diagnostics-fixit-info");
569 // Enable -fdiagnostics-show-option by default.
570 if (Args.hasFlag(options::OPT_fdiagnostics_show_option,
571 options::OPT_fno_diagnostics_show_option))
572 CmdArgs.push_back("-fdiagnostics-show-option");
573 if (!Args.hasFlag(options::OPT_fcolor_diagnostics,
574 options::OPT_fno_color_diagnostics))
575 CmdArgs.push_back("-fno-color-diagnostics");
576 if (!Args.hasFlag(options::OPT_fshow_source_location,
577 options::OPT_fno_show_source_location))
578 CmdArgs.push_back("-fno-show-source-location");
580 // -fdollars-in-identifiers default varies depending on platform and
581 // language; only pass if specified.
582 if (Arg *A = Args.getLastArg(options::OPT_fdollars_in_identifiers,
583 options::OPT_fno_dollars_in_identifiers)) {
584 if (A->getOption().matches(options::OPT_fdollars_in_identifiers))
585 CmdArgs.push_back("-fdollars-in-identifiers=1");
587 CmdArgs.push_back("-fdollars-in-identifiers=0");
590 // -funit-at-a-time is default, and we don't support -fno-unit-at-a-time for
591 // practical purposes.
592 if (Arg *A = Args.getLastArg(options::OPT_funit_at_a_time,
593 options::OPT_fno_unit_at_a_time)) {
594 if (A->getOption().matches(options::OPT_fno_unit_at_a_time))
595 D.Diag(clang::diag::err_drv_clang_unsupported) << A->getAsString(Args);
598 Args.AddLastArg(CmdArgs, options::OPT_dM);
599 Args.AddLastArg(CmdArgs, options::OPT_dD);
601 Args.AddAllArgValues(CmdArgs, options::OPT_Xclang);
602 Args.AddAllArgValues(CmdArgs, options::OPT_mllvm);
604 if (Output.getType() == types::TY_Dependencies) {
605 // Handled with other dependency code.
606 } else if (Output.isPipe()) {
607 CmdArgs.push_back("-o");
608 CmdArgs.push_back("-");
609 } else if (Output.isFilename()) {
610 CmdArgs.push_back("-o");
611 CmdArgs.push_back(Output.getFilename());
613 assert(Output.isNothing() && "Invalid output.");
616 for (InputInfoList::const_iterator
617 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
618 const InputInfo &II = *it;
619 CmdArgs.push_back("-x");
620 CmdArgs.push_back(types::getTypeName(II.getType()));
622 CmdArgs.push_back("-");
623 else if (II.isFilename())
624 CmdArgs.push_back(II.getFilename());
626 II.getInputArg().renderAsInput(Args, CmdArgs);
630 Args.MakeArgString(getToolChain().GetProgramPath(C, "clang-cc").c_str());
631 Dest.addCommand(new Command(JA, Exec, CmdArgs));
633 // Explicitly warn that these options are unsupported, even though
634 // we are allowing compilation to continue.
635 // FIXME: Use iterator.
636 for (ArgList::const_iterator
637 it = Args.begin(), ie = Args.end(); it != ie; ++it) {
639 if (A->getOption().matches(options::OPT_pg)) {
641 D.Diag(clang::diag::warn_drv_clang_unsupported)
642 << A->getAsString(Args);
646 // Claim some arguments which clang supports automatically.
648 // -fpch-preprocess is used with gcc to add a special marker in the
649 // -output to include the PCH file. Clang's PTH solution is
650 // -completely transparent, so we do not need to deal with it at
652 Args.ClaimAllArgs(options::OPT_fpch_preprocess);
654 // Claim some arguments which clang doesn't support, but we don't
655 // care to warn the user about.
657 // FIXME: Use iterator.
658 for (ArgList::const_iterator
659 it = Args.begin(), ie = Args.end(); it != ie; ++it) {
661 if (A->getOption().matches(options::OPT_clang_ignored_f_Group) ||
662 A->getOption().matches(options::OPT_clang_ignored_m_Group))
667 void gcc::Common::ConstructJob(Compilation &C, const JobAction &JA,
669 const InputInfo &Output,
670 const InputInfoList &Inputs,
672 const char *LinkingOutput) const {
673 const Driver &D = getToolChain().getHost().getDriver();
674 ArgStringList CmdArgs;
676 for (ArgList::const_iterator
677 it = Args.begin(), ie = Args.end(); it != ie; ++it) {
679 if (A->getOption().hasForwardToGCC()) {
680 // It is unfortunate that we have to claim here, as this means
681 // we will basically never report anything interesting for
682 // platforms using a generic gcc, even if we are just using gcc
683 // to get to the assembler.
685 A->render(Args, CmdArgs);
689 RenderExtraToolArgs(CmdArgs);
691 // If using a driver driver, force the arch.
692 const std::string &Arch = getToolChain().getArchName();
693 if (getToolChain().getHost().useDriverDriver()) {
694 CmdArgs.push_back("-arch");
696 // FIXME: Remove these special cases.
697 if (Arch == "powerpc")
698 CmdArgs.push_back("ppc");
699 else if (Arch == "powerpc64")
700 CmdArgs.push_back("ppc64");
702 CmdArgs.push_back(Args.MakeArgString(Arch.c_str()));
705 // Try to force gcc to match the tool chain we want, if we recognize
708 // FIXME: The triple class should directly provide the information we want
710 if (Arch == "i386" || Arch == "powerpc")
711 CmdArgs.push_back("-m32");
712 else if (Arch == "x86_64" || Arch == "powerpc64")
713 CmdArgs.push_back("-m64");
715 if (Output.isPipe()) {
716 CmdArgs.push_back("-o");
717 CmdArgs.push_back("-");
718 } else if (Output.isFilename()) {
719 CmdArgs.push_back("-o");
720 CmdArgs.push_back(Output.getFilename());
722 assert(Output.isNothing() && "Unexpected output");
723 CmdArgs.push_back("-fsyntax-only");
727 // Only pass -x if gcc will understand it; otherwise hope gcc
728 // understands the suffix correctly. The main use case this would go
729 // wrong in is for linker inputs if they happened to have an odd
730 // suffix; really the only way to get this to happen is a command
731 // like '-x foobar a.c' which will treat a.c like a linker input.
733 // FIXME: For the linker case specifically, can we safely convert
734 // inputs into '-Wl,' options?
735 for (InputInfoList::const_iterator
736 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
737 const InputInfo &II = *it;
739 // Don't try to pass LLVM inputs to a generic gcc.
740 if (II.getType() == types::TY_LLVMBC)
741 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
742 << getToolChain().getTripleString().c_str();
744 if (types::canTypeBeUserSpecified(II.getType())) {
745 CmdArgs.push_back("-x");
746 CmdArgs.push_back(types::getTypeName(II.getType()));
750 CmdArgs.push_back("-");
751 else if (II.isFilename())
752 CmdArgs.push_back(II.getFilename());
754 // Don't render as input, we need gcc to do the translations.
755 II.getInputArg().render(Args, CmdArgs);
758 const char *GCCName =
759 getToolChain().getHost().getDriver().CCCGenericGCCName.c_str();
761 Args.MakeArgString(getToolChain().GetProgramPath(C, GCCName).c_str());
762 Dest.addCommand(new Command(JA, Exec, CmdArgs));
765 void gcc::Preprocess::RenderExtraToolArgs(ArgStringList &CmdArgs) const {
766 CmdArgs.push_back("-E");
769 void gcc::Precompile::RenderExtraToolArgs(ArgStringList &CmdArgs) const {
770 // The type is good enough.
773 void gcc::Compile::RenderExtraToolArgs(ArgStringList &CmdArgs) const {
774 CmdArgs.push_back("-S");
777 void gcc::Assemble::RenderExtraToolArgs(ArgStringList &CmdArgs) const {
778 CmdArgs.push_back("-c");
781 void gcc::Link::RenderExtraToolArgs(ArgStringList &CmdArgs) const {
782 // The types are (hopefully) good enough.
785 const char *darwin::CC1::getCC1Name(types::ID Type) const {
788 assert(0 && "Unexpected type for Darwin CC1 tool.");
790 case types::TY_C: case types::TY_CHeader:
791 case types::TY_PP_C: case types::TY_PP_CHeader:
793 case types::TY_ObjC: case types::TY_ObjCHeader:
794 case types::TY_PP_ObjC: case types::TY_PP_ObjCHeader:
796 case types::TY_CXX: case types::TY_CXXHeader:
797 case types::TY_PP_CXX: case types::TY_PP_CXXHeader:
799 case types::TY_ObjCXX: case types::TY_ObjCXXHeader:
800 case types::TY_PP_ObjCXX: case types::TY_PP_ObjCXXHeader:
805 const char *darwin::CC1::getBaseInputName(const ArgList &Args,
806 const InputInfoList &Inputs) {
807 llvm::sys::Path P(Inputs[0].getBaseInput());
808 return Args.MakeArgString(P.getLast().c_str());
811 const char *darwin::CC1::getBaseInputStem(const ArgList &Args,
812 const InputInfoList &Inputs) {
813 const char *Str = getBaseInputName(Args, Inputs);
815 if (const char *End = strchr(Str, '.'))
816 return Args.MakeArgString(std::string(Str, End).c_str());
822 darwin::CC1::getDependencyFileName(const ArgList &Args,
823 const InputInfoList &Inputs) {
824 // FIXME: Think about this more.
827 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
828 std::string Str(OutputOpt->getValue(Args));
830 Res = Str.substr(0, Str.rfind('.'));
832 Res = darwin::CC1::getBaseInputStem(Args, Inputs);
834 return Args.MakeArgString((Res + ".d").c_str());
837 void darwin::CC1::AddCC1Args(const ArgList &Args,
838 ArgStringList &CmdArgs) const {
839 // Derived from cc1 spec.
841 // FIXME: -fapple-kext seems to disable this too. Investigate.
842 if (!Args.hasArg(options::OPT_mkernel) && !Args.hasArg(options::OPT_static) &&
843 !Args.hasArg(options::OPT_mdynamic_no_pic))
844 CmdArgs.push_back("-fPIC");
846 // gcc has some code here to deal with when no -mmacosx-version-min
847 // and no -miphoneos-version-min is present, but this never happens
848 // due to tool chain specific argument translation.
850 // FIXME: Remove mthumb
851 // FIXME: Remove mno-thumb
852 // FIXME: Remove faltivec
853 // FIXME: Remove mno-fused-madd
854 // FIXME: Remove mlong-branch
855 // FIXME: Remove mlongcall
856 // FIXME: Remove mcpu=G4
857 // FIXME: Remove mcpu=G5
859 if (Args.hasArg(options::OPT_g_Flag) &&
860 !Args.hasArg(options::OPT_fno_eliminate_unused_debug_symbols))
861 CmdArgs.push_back("-feliminate-unused-debug-symbols");
864 void darwin::CC1::AddCC1OptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
865 const InputInfoList &Inputs,
866 const ArgStringList &OutputArgs) const {
867 const Driver &D = getToolChain().getHost().getDriver();
869 // Derived from cc1_options spec.
870 if (Args.hasArg(options::OPT_fast) ||
871 Args.hasArg(options::OPT_fastf) ||
872 Args.hasArg(options::OPT_fastcp))
873 CmdArgs.push_back("-O3");
875 if (Arg *A = Args.getLastArg(options::OPT_pg))
876 if (Args.hasArg(options::OPT_fomit_frame_pointer))
877 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
878 << A->getAsString(Args) << "-fomit-frame-pointer";
880 AddCC1Args(Args, CmdArgs);
882 if (!Args.hasArg(options::OPT_Q))
883 CmdArgs.push_back("-quiet");
885 CmdArgs.push_back("-dumpbase");
886 CmdArgs.push_back(darwin::CC1::getBaseInputName(Args, Inputs));
888 Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
890 Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
891 Args.AddAllArgs(CmdArgs, options::OPT_a_Group);
893 // FIXME: The goal is to use the user provided -o if that is our
894 // final output, otherwise to drive from the original input
895 // name. Find a clean way to go about this.
896 if ((Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) &&
897 Args.hasArg(options::OPT_o)) {
898 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
899 CmdArgs.push_back("-auxbase-strip");
900 CmdArgs.push_back(OutputOpt->getValue(Args));
902 CmdArgs.push_back("-auxbase");
903 CmdArgs.push_back(darwin::CC1::getBaseInputStem(Args, Inputs));
906 Args.AddAllArgs(CmdArgs, options::OPT_g_Group);
908 Args.AddAllArgs(CmdArgs, options::OPT_O);
909 // FIXME: -Wall is getting some special treatment. Investigate.
910 Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
911 Args.AddLastArg(CmdArgs, options::OPT_w);
912 Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
913 options::OPT_trigraphs);
914 if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
915 // Honor -std-default.
916 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
917 "-std=", /*Joined=*/true);
920 if (Args.hasArg(options::OPT_v))
921 CmdArgs.push_back("-version");
922 if (Args.hasArg(options::OPT_pg))
923 CmdArgs.push_back("-p");
924 Args.AddLastArg(CmdArgs, options::OPT_p);
926 // The driver treats -fsyntax-only specially.
927 Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
929 Args.AddAllArgs(CmdArgs, options::OPT_undef);
930 if (Args.hasArg(options::OPT_Qn))
931 CmdArgs.push_back("-fno-ident");
933 // FIXME: This isn't correct.
934 //Args.AddLastArg(CmdArgs, options::OPT__help)
935 //Args.AddLastArg(CmdArgs, options::OPT__targetHelp)
937 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
939 // FIXME: Still don't get what is happening here. Investigate.
940 Args.AddAllArgs(CmdArgs, options::OPT__param);
942 if (Args.hasArg(options::OPT_fmudflap) ||
943 Args.hasArg(options::OPT_fmudflapth)) {
944 CmdArgs.push_back("-fno-builtin");
945 CmdArgs.push_back("-fno-merge-constants");
948 if (Args.hasArg(options::OPT_coverage)) {
949 CmdArgs.push_back("-fprofile-arcs");
950 CmdArgs.push_back("-ftest-coverage");
953 if (types::isCXX(Inputs[0].getType()))
954 CmdArgs.push_back("-D__private_extern__=extern");
957 void darwin::CC1::AddCPPOptionsArgs(const ArgList &Args, ArgStringList &CmdArgs,
958 const InputInfoList &Inputs,
959 const ArgStringList &OutputArgs) const {
960 // Derived from cpp_options
961 AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
963 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
965 AddCC1Args(Args, CmdArgs);
967 // NOTE: The code below has some commonality with cpp_options, but
968 // in classic gcc style ends up sending things in different
969 // orders. This may be a good merge candidate once we drop pedantic
972 Args.AddAllArgs(CmdArgs, options::OPT_m_Group);
973 Args.AddAllArgs(CmdArgs, options::OPT_std_EQ, options::OPT_ansi,
974 options::OPT_trigraphs);
975 if (!Args.getLastArg(options::OPT_std_EQ, options::OPT_ansi)) {
976 // Honor -std-default.
977 Args.AddAllArgsTranslated(CmdArgs, options::OPT_std_default_EQ,
978 "-std=", /*Joined=*/true);
980 Args.AddAllArgs(CmdArgs, options::OPT_W_Group, options::OPT_pedantic_Group);
981 Args.AddLastArg(CmdArgs, options::OPT_w);
983 // The driver treats -fsyntax-only specially.
984 Args.AddAllArgs(CmdArgs, options::OPT_f_Group, options::OPT_fsyntax_only);
986 if (Args.hasArg(options::OPT_g_Group) && !Args.hasArg(options::OPT_g0) &&
987 !Args.hasArg(options::OPT_fno_working_directory))
988 CmdArgs.push_back("-fworking-directory");
990 Args.AddAllArgs(CmdArgs, options::OPT_O);
991 Args.AddAllArgs(CmdArgs, options::OPT_undef);
992 if (Args.hasArg(options::OPT_save_temps))
993 CmdArgs.push_back("-fpch-preprocess");
996 void darwin::CC1::AddCPPUniqueOptionsArgs(const ArgList &Args,
997 ArgStringList &CmdArgs,
998 const InputInfoList &Inputs) const
1000 const Driver &D = getToolChain().getHost().getDriver();
1002 // Derived from cpp_unique_options.
1004 if ((A = Args.getLastArg(options::OPT_C)) ||
1005 (A = Args.getLastArg(options::OPT_CC))) {
1006 if (!Args.hasArg(options::OPT_E))
1007 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
1008 << A->getAsString(Args) << "-E";
1010 if (!Args.hasArg(options::OPT_Q))
1011 CmdArgs.push_back("-quiet");
1012 Args.AddAllArgs(CmdArgs, options::OPT_nostdinc);
1013 Args.AddLastArg(CmdArgs, options::OPT_v);
1014 Args.AddAllArgs(CmdArgs, options::OPT_I_Group, options::OPT_F);
1015 Args.AddLastArg(CmdArgs, options::OPT_P);
1017 // FIXME: Handle %I properly.
1018 if (getToolChain().getArchName() == "x86_64") {
1019 CmdArgs.push_back("-imultilib");
1020 CmdArgs.push_back("x86_64");
1023 if (Args.hasArg(options::OPT_MD)) {
1024 CmdArgs.push_back("-MD");
1025 CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
1028 if (Args.hasArg(options::OPT_MMD)) {
1029 CmdArgs.push_back("-MMD");
1030 CmdArgs.push_back(darwin::CC1::getDependencyFileName(Args, Inputs));
1033 Args.AddLastArg(CmdArgs, options::OPT_M);
1034 Args.AddLastArg(CmdArgs, options::OPT_MM);
1035 Args.AddAllArgs(CmdArgs, options::OPT_MF);
1036 Args.AddLastArg(CmdArgs, options::OPT_MG);
1037 Args.AddLastArg(CmdArgs, options::OPT_MP);
1038 Args.AddAllArgs(CmdArgs, options::OPT_MQ);
1039 Args.AddAllArgs(CmdArgs, options::OPT_MT);
1040 if (!Args.hasArg(options::OPT_M) && !Args.hasArg(options::OPT_MM) &&
1041 (Args.hasArg(options::OPT_MD) || Args.hasArg(options::OPT_MMD))) {
1042 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o)) {
1043 CmdArgs.push_back("-MQ");
1044 CmdArgs.push_back(OutputOpt->getValue(Args));
1048 Args.AddLastArg(CmdArgs, options::OPT_remap);
1049 if (Args.hasArg(options::OPT_g3))
1050 CmdArgs.push_back("-dD");
1051 Args.AddLastArg(CmdArgs, options::OPT_H);
1053 AddCPPArgs(Args, CmdArgs);
1055 Args.AddAllArgs(CmdArgs, options::OPT_D, options::OPT_U, options::OPT_A);
1056 Args.AddAllArgs(CmdArgs, options::OPT_i_Group);
1058 for (InputInfoList::const_iterator
1059 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
1060 const InputInfo &II = *it;
1063 CmdArgs.push_back("-");
1065 CmdArgs.push_back(II.getFilename());
1068 Args.AddAllArgValues(CmdArgs, options::OPT_Wp_COMMA,
1069 options::OPT_Xpreprocessor);
1071 if (Args.hasArg(options::OPT_fmudflap)) {
1072 CmdArgs.push_back("-D_MUDFLAP");
1073 CmdArgs.push_back("-include");
1074 CmdArgs.push_back("mf-runtime.h");
1077 if (Args.hasArg(options::OPT_fmudflapth)) {
1078 CmdArgs.push_back("-D_MUDFLAP");
1079 CmdArgs.push_back("-D_MUDFLAPTH");
1080 CmdArgs.push_back("-include");
1081 CmdArgs.push_back("mf-runtime.h");
1085 void darwin::CC1::AddCPPArgs(const ArgList &Args,
1086 ArgStringList &CmdArgs) const {
1087 // Derived from cpp spec.
1089 if (Args.hasArg(options::OPT_static)) {
1090 // The gcc spec is broken here, it refers to dynamic but
1091 // that has been translated. Start by being bug compatible.
1093 // if (!Args.hasArg(arglist.parser.dynamicOption))
1094 CmdArgs.push_back("-D__STATIC__");
1096 CmdArgs.push_back("-D__DYNAMIC__");
1098 if (Args.hasArg(options::OPT_pthread))
1099 CmdArgs.push_back("-D_REENTRANT");
1102 void darwin::Preprocess::ConstructJob(Compilation &C, const JobAction &JA,
1103 Job &Dest, const InputInfo &Output,
1104 const InputInfoList &Inputs,
1105 const ArgList &Args,
1106 const char *LinkingOutput) const {
1107 ArgStringList CmdArgs;
1109 assert(Inputs.size() == 1 && "Unexpected number of inputs!");
1111 CmdArgs.push_back("-E");
1113 if (Args.hasArg(options::OPT_traditional) ||
1114 Args.hasArg(options::OPT_ftraditional) ||
1115 Args.hasArg(options::OPT_traditional_cpp))
1116 CmdArgs.push_back("-traditional-cpp");
1118 ArgStringList OutputArgs;
1119 if (Output.isFilename()) {
1120 OutputArgs.push_back("-o");
1121 OutputArgs.push_back(Output.getFilename());
1123 assert(Output.isPipe() && "Unexpected CC1 output.");
1126 if (Args.hasArg(options::OPT_E)) {
1127 AddCPPOptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
1129 AddCPPOptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
1130 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
1133 Args.AddAllArgs(CmdArgs, options::OPT_d_Group);
1135 const char *CC1Name = getCC1Name(Inputs[0].getType());
1137 Args.MakeArgString(getToolChain().GetProgramPath(C, CC1Name).c_str());
1138 Dest.addCommand(new Command(JA, Exec, CmdArgs));
1141 void darwin::Compile::ConstructJob(Compilation &C, const JobAction &JA,
1142 Job &Dest, const InputInfo &Output,
1143 const InputInfoList &Inputs,
1144 const ArgList &Args,
1145 const char *LinkingOutput) const {
1146 const Driver &D = getToolChain().getHost().getDriver();
1147 ArgStringList CmdArgs;
1149 assert(Inputs.size() == 1 && "Unexpected number of inputs!");
1151 types::ID InputType = Inputs[0].getType();
1153 if ((A = Args.getLastArg(options::OPT_traditional)) ||
1154 (A = Args.getLastArg(options::OPT_ftraditional)))
1155 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
1156 << A->getAsString(Args) << "-E";
1158 if (Output.getType() == types::TY_LLVMAsm)
1159 CmdArgs.push_back("-emit-llvm");
1160 else if (Output.getType() == types::TY_LLVMBC)
1161 CmdArgs.push_back("-emit-llvm-bc");
1163 ArgStringList OutputArgs;
1164 if (Output.getType() != types::TY_PCH) {
1165 OutputArgs.push_back("-o");
1166 if (Output.isPipe())
1167 OutputArgs.push_back("-");
1168 else if (Output.isNothing())
1169 OutputArgs.push_back("/dev/null");
1171 OutputArgs.push_back(Output.getFilename());
1174 // There is no need for this level of compatibility, but it makes
1176 bool OutputArgsEarly = (Args.hasArg(options::OPT_fsyntax_only) ||
1177 Args.hasArg(options::OPT_S));
1179 if (types::getPreprocessedType(InputType) != types::TY_INVALID) {
1180 AddCPPUniqueOptionsArgs(Args, CmdArgs, Inputs);
1181 if (OutputArgsEarly) {
1182 AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
1184 AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
1185 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
1188 CmdArgs.push_back("-fpreprocessed");
1190 // FIXME: There is a spec command to remove
1191 // -fpredictive-compilation args here. Investigate.
1193 for (InputInfoList::const_iterator
1194 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
1195 const InputInfo &II = *it;
1198 CmdArgs.push_back("-");
1200 CmdArgs.push_back(II.getFilename());
1203 if (OutputArgsEarly) {
1204 AddCC1OptionsArgs(Args, CmdArgs, Inputs, OutputArgs);
1206 AddCC1OptionsArgs(Args, CmdArgs, Inputs, ArgStringList());
1207 CmdArgs.append(OutputArgs.begin(), OutputArgs.end());
1211 if (Output.getType() == types::TY_PCH) {
1212 assert(Output.isFilename() && "Invalid PCH output.");
1214 CmdArgs.push_back("-o");
1215 // NOTE: gcc uses a temp .s file for this, but there doesn't seem
1216 // to be a good reason.
1217 CmdArgs.push_back("/dev/null");
1219 CmdArgs.push_back("--output-pch=");
1220 CmdArgs.push_back(Output.getFilename());
1223 const char *CC1Name = getCC1Name(Inputs[0].getType());
1225 Args.MakeArgString(getToolChain().GetProgramPath(C, CC1Name).c_str());
1226 Dest.addCommand(new Command(JA, Exec, CmdArgs));
1229 void darwin::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
1230 Job &Dest, const InputInfo &Output,
1231 const InputInfoList &Inputs,
1232 const ArgList &Args,
1233 const char *LinkingOutput) const {
1234 ArgStringList CmdArgs;
1236 assert(Inputs.size() == 1 && "Unexpected number of inputs.");
1237 const InputInfo &Input = Inputs[0];
1239 // Bit of a hack, this is only used for original inputs.
1241 // FIXME: This is broken for preprocessed .s inputs.
1242 if (Input.isFilename() &&
1243 strcmp(Input.getFilename(), Input.getBaseInput()) == 0) {
1244 if (Args.hasArg(options::OPT_gstabs))
1245 CmdArgs.push_back("--gstabs");
1246 else if (Args.hasArg(options::OPT_g_Group))
1247 CmdArgs.push_back("--gdwarf2");
1250 // Derived from asm spec.
1251 CmdArgs.push_back("-arch");
1252 CmdArgs.push_back(Args.MakeArgString(getToolChain().getArchName().c_str()));
1254 CmdArgs.push_back("-force_cpusubtype_ALL");
1255 if ((Args.hasArg(options::OPT_mkernel) ||
1256 Args.hasArg(options::OPT_static) ||
1257 Args.hasArg(options::OPT_fapple_kext)) &&
1258 !Args.hasArg(options::OPT_dynamic))
1259 CmdArgs.push_back("-static");
1261 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
1262 options::OPT_Xassembler);
1264 assert(Output.isFilename() && "Unexpected lipo output.");
1265 CmdArgs.push_back("-o");
1266 CmdArgs.push_back(Output.getFilename());
1268 if (Input.isPipe()) {
1269 CmdArgs.push_back("-");
1271 assert(Input.isFilename() && "Invalid input.");
1272 CmdArgs.push_back(Input.getFilename());
1275 // asm_final spec is empty.
1278 Args.MakeArgString(getToolChain().GetProgramPath(C, "as").c_str());
1279 Dest.addCommand(new Command(JA, Exec, CmdArgs));
1282 /// Helper routine for seeing if we should use dsymutil; this is a
1283 /// gcc compatible hack, we should remove it and use the input
1284 /// type information.
1285 static bool isSourceSuffix(const char *Str) {
1286 // match: 'C', 'CPP', 'c', 'cc', 'cp', 'c++', 'cpp', 'cxx', 'm',
1288 switch (strlen(Str)) {
1292 return (memcmp(Str, "C", 1) == 0 ||
1293 memcmp(Str, "c", 1) == 0 ||
1294 memcmp(Str, "m", 1) == 0);
1296 return (memcmp(Str, "cc", 2) == 0 ||
1297 memcmp(Str, "cp", 2) == 0 ||
1298 memcmp(Str, "mm", 2) == 0);
1300 return (memcmp(Str, "CPP", 3) == 0 ||
1301 memcmp(Str, "c++", 3) == 0 ||
1302 memcmp(Str, "cpp", 3) == 0 ||
1303 memcmp(Str, "cxx", 3) == 0);
1307 static bool isMacosxVersionLT(unsigned (&A)[3], unsigned (&B)[3]) {
1308 for (unsigned i=0; i < 3; ++i) {
1309 if (A[i] > B[i]) return false;
1310 if (A[i] < B[i]) return true;
1315 static bool isMacosxVersionLT(unsigned (&A)[3],
1316 unsigned V0, unsigned V1=0, unsigned V2=0) {
1317 unsigned B[3] = { V0, V1, V2 };
1318 return isMacosxVersionLT(A, B);
1321 const toolchains::Darwin_X86 &darwin::Link::getDarwinToolChain() const {
1322 return reinterpret_cast<const toolchains::Darwin_X86&>(getToolChain());
1325 void darwin::Link::AddDarwinArch(const ArgList &Args,
1326 ArgStringList &CmdArgs) const {
1327 // Derived from darwin_arch spec.
1328 CmdArgs.push_back("-arch");
1329 CmdArgs.push_back(Args.MakeArgString(getToolChain().getArchName().c_str()));
1332 void darwin::Link::AddDarwinSubArch(const ArgList &Args,
1333 ArgStringList &CmdArgs) const {
1334 // Derived from darwin_subarch spec, not sure what the distinction
1335 // exists for but at least for this chain it is the same.
1336 AddDarwinArch(Args, CmdArgs);
1339 void darwin::Link::AddLinkArgs(const ArgList &Args,
1340 ArgStringList &CmdArgs) const {
1341 const Driver &D = getToolChain().getHost().getDriver();
1343 // Derived from the "link" spec.
1344 Args.AddAllArgs(CmdArgs, options::OPT_static);
1345 if (!Args.hasArg(options::OPT_static))
1346 CmdArgs.push_back("-dynamic");
1347 if (Args.hasArg(options::OPT_fgnu_runtime)) {
1348 // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu
1349 // here. How do we wish to handle such things?
1352 if (!Args.hasArg(options::OPT_dynamiclib)) {
1353 if (Args.hasArg(options::OPT_force__cpusubtype__ALL)) {
1354 AddDarwinArch(Args, CmdArgs);
1355 CmdArgs.push_back("-force_cpusubtype_ALL");
1357 AddDarwinSubArch(Args, CmdArgs);
1359 Args.AddLastArg(CmdArgs, options::OPT_bundle);
1360 Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader);
1361 Args.AddAllArgs(CmdArgs, options::OPT_client__name);
1364 if ((A = Args.getLastArg(options::OPT_compatibility__version)) ||
1365 (A = Args.getLastArg(options::OPT_current__version)) ||
1366 (A = Args.getLastArg(options::OPT_install__name)))
1367 D.Diag(clang::diag::err_drv_argument_only_allowed_with)
1368 << A->getAsString(Args) << "-dynamiclib";
1370 Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace);
1371 Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs);
1372 Args.AddLastArg(CmdArgs, options::OPT_private__bundle);
1374 CmdArgs.push_back("-dylib");
1377 if ((A = Args.getLastArg(options::OPT_bundle)) ||
1378 (A = Args.getLastArg(options::OPT_bundle__loader)) ||
1379 (A = Args.getLastArg(options::OPT_client__name)) ||
1380 (A = Args.getLastArg(options::OPT_force__flat__namespace)) ||
1381 (A = Args.getLastArg(options::OPT_keep__private__externs)) ||
1382 (A = Args.getLastArg(options::OPT_private__bundle)))
1383 D.Diag(clang::diag::err_drv_argument_not_allowed_with)
1384 << A->getAsString(Args) << "-dynamiclib";
1386 Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version,
1387 "-dylib_compatibility_version");
1388 Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version,
1389 "-dylib_current_version");
1391 if (Args.hasArg(options::OPT_force__cpusubtype__ALL)) {
1392 AddDarwinArch(Args, CmdArgs);
1393 // NOTE: We don't add -force_cpusubtype_ALL on this path. Ok.
1395 AddDarwinSubArch(Args, CmdArgs);
1397 Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name,
1398 "-dylib_install_name");
1401 Args.AddLastArg(CmdArgs, options::OPT_all__load);
1402 Args.AddAllArgs(CmdArgs, options::OPT_allowable__client);
1403 Args.AddLastArg(CmdArgs, options::OPT_bind__at__load);
1404 Args.AddLastArg(CmdArgs, options::OPT_dead__strip);
1405 Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms);
1406 Args.AddAllArgs(CmdArgs, options::OPT_dylib__file);
1407 Args.AddLastArg(CmdArgs, options::OPT_dynamic);
1408 Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list);
1409 Args.AddLastArg(CmdArgs, options::OPT_flat__namespace);
1410 Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names);
1411 Args.AddAllArgs(CmdArgs, options::OPT_image__base);
1412 Args.AddAllArgs(CmdArgs, options::OPT_init);
1414 if (!Args.hasArg(options::OPT_mmacosx_version_min_EQ)) {
1415 if (!Args.hasArg(options::OPT_miphoneos_version_min_EQ)) {
1416 // FIXME: I don't understand what is going on here. This is
1417 // supposed to come from darwin_ld_minversion, but gcc doesn't
1418 // seem to be following that; it must be getting overridden
1420 CmdArgs.push_back("-macosx_version_min");
1421 CmdArgs.push_back(getDarwinToolChain().getMacosxVersionStr());
1424 // Adding all arguments doesn't make sense here but this is what
1426 Args.AddAllArgsTranslated(CmdArgs, options::OPT_mmacosx_version_min_EQ,
1427 "-macosx_version_min");
1430 Args.AddAllArgsTranslated(CmdArgs, options::OPT_miphoneos_version_min_EQ,
1431 "-iphoneos_version_min");
1432 Args.AddLastArg(CmdArgs, options::OPT_nomultidefs);
1433 Args.AddLastArg(CmdArgs, options::OPT_multi__module);
1434 Args.AddLastArg(CmdArgs, options::OPT_single__module);
1435 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined);
1436 Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused);
1438 if (Args.hasArg(options::OPT_fpie))
1439 CmdArgs.push_back("-pie");
1441 Args.AddLastArg(CmdArgs, options::OPT_prebind);
1442 Args.AddLastArg(CmdArgs, options::OPT_noprebind);
1443 Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding);
1444 Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules);
1445 Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs);
1446 Args.AddAllArgs(CmdArgs, options::OPT_sectcreate);
1447 Args.AddAllArgs(CmdArgs, options::OPT_sectorder);
1448 Args.AddAllArgs(CmdArgs, options::OPT_seg1addr);
1449 Args.AddAllArgs(CmdArgs, options::OPT_segprot);
1450 Args.AddAllArgs(CmdArgs, options::OPT_segaddr);
1451 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr);
1452 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr);
1453 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table);
1454 Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename);
1455 Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
1456 Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);
1457 Args.AddAllArgsTranslated(CmdArgs, options::OPT_isysroot, "-syslibroot");
1458 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);
1459 Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints);
1460 Args.AddAllArgs(CmdArgs, options::OPT_umbrella);
1461 Args.AddAllArgs(CmdArgs, options::OPT_undefined);
1462 Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list);
1463 Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches);
1465 if (!Args.hasArg(options::OPT_weak__reference__mismatches)) {
1466 CmdArgs.push_back("-weak_reference_mismatches");
1467 CmdArgs.push_back("non-weak");
1470 Args.AddLastArg(CmdArgs, options::OPT_X_Flag);
1471 Args.AddAllArgs(CmdArgs, options::OPT_y);
1472 Args.AddLastArg(CmdArgs, options::OPT_w);
1473 Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size);
1474 Args.AddAllArgs(CmdArgs, options::OPT_segs__read__);
1475 Args.AddLastArg(CmdArgs, options::OPT_seglinkedit);
1476 Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit);
1477 Args.AddAllArgs(CmdArgs, options::OPT_sectalign);
1478 Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols);
1479 Args.AddAllArgs(CmdArgs, options::OPT_segcreate);
1480 Args.AddLastArg(CmdArgs, options::OPT_whyload);
1481 Args.AddLastArg(CmdArgs, options::OPT_whatsloaded);
1482 Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name);
1483 Args.AddLastArg(CmdArgs, options::OPT_dylinker);
1484 Args.AddLastArg(CmdArgs, options::OPT_Mach);
1487 void darwin::Link::ConstructJob(Compilation &C, const JobAction &JA,
1488 Job &Dest, const InputInfo &Output,
1489 const InputInfoList &Inputs,
1490 const ArgList &Args,
1491 const char *LinkingOutput) const {
1492 assert(Output.getType() == types::TY_Image && "Invalid linker output type.");
1493 // The logic here is derived from gcc's behavior; most of which
1494 // comes from specs (starting with link_command). Consult gcc for
1495 // more information.
1497 // FIXME: The spec references -fdump= which seems to have
1500 ArgStringList CmdArgs;
1502 // I'm not sure why this particular decomposition exists in gcc, but
1503 // we follow suite for ease of comparison.
1504 AddLinkArgs(Args, CmdArgs);
1506 // FIXME: gcc has %{x} in here. How could this ever happen? Cruft?
1507 Args.AddAllArgs(CmdArgs, options::OPT_d_Flag);
1508 Args.AddAllArgs(CmdArgs, options::OPT_s);
1509 Args.AddAllArgs(CmdArgs, options::OPT_t);
1510 Args.AddAllArgs(CmdArgs, options::OPT_Z_Flag);
1511 Args.AddAllArgs(CmdArgs, options::OPT_u_Group);
1512 Args.AddAllArgs(CmdArgs, options::OPT_A);
1513 Args.AddLastArg(CmdArgs, options::OPT_e);
1514 Args.AddAllArgs(CmdArgs, options::OPT_m_Separate);
1515 Args.AddAllArgs(CmdArgs, options::OPT_r);
1517 // FIXME: This is just being pedantically bug compatible, gcc
1518 // doesn't *mean* to forward this, it just does (yay for pattern
1519 // matching). It doesn't work, of course.
1520 Args.AddAllArgs(CmdArgs, options::OPT_object);
1522 CmdArgs.push_back("-o");
1523 CmdArgs.push_back(Output.getFilename());
1525 unsigned MacosxVersion[3];
1526 if (Arg *A = Args.getLastArg(options::OPT_mmacosx_version_min_EQ)) {
1528 if (!Driver::GetReleaseVersion(A->getValue(Args), MacosxVersion[0],
1529 MacosxVersion[1], MacosxVersion[2],
1532 const Driver &D = getToolChain().getHost().getDriver();
1533 D.Diag(clang::diag::err_drv_invalid_version_number)
1534 << A->getAsString(Args);
1537 getDarwinToolChain().getMacosxVersion(MacosxVersion);
1540 if (!Args.hasArg(options::OPT_A) &&
1541 !Args.hasArg(options::OPT_nostdlib) &&
1542 !Args.hasArg(options::OPT_nostartfiles)) {
1543 // Derived from startfile spec.
1544 if (Args.hasArg(options::OPT_dynamiclib)) {
1545 // Derived from darwin_dylib1 spec.
1546 if (isMacosxVersionLT(MacosxVersion, 10, 5))
1547 CmdArgs.push_back("-ldylib1.o");
1548 else if (isMacosxVersionLT(MacosxVersion, 10, 6))
1549 CmdArgs.push_back("-ldylib1.10.5.o");
1551 if (Args.hasArg(options::OPT_bundle)) {
1552 if (!Args.hasArg(options::OPT_static)) {
1553 // Derived from darwin_bundle1 spec.
1554 if (isMacosxVersionLT(MacosxVersion, 10, 6))
1555 CmdArgs.push_back("-lbundle1.o");
1558 if (Args.hasArg(options::OPT_pg)) {
1559 if (Args.hasArg(options::OPT_static) ||
1560 Args.hasArg(options::OPT_object) ||
1561 Args.hasArg(options::OPT_preload)) {
1562 CmdArgs.push_back("-lgcrt0.o");
1564 CmdArgs.push_back("-lgcrt1.o");
1566 // darwin_crt2 spec is empty.
1569 if (Args.hasArg(options::OPT_static) ||
1570 Args.hasArg(options::OPT_object) ||
1571 Args.hasArg(options::OPT_preload)) {
1572 CmdArgs.push_back("-lcrt0.o");
1574 // Derived from darwin_crt1 spec.
1575 if (isMacosxVersionLT(MacosxVersion, 10, 5))
1576 CmdArgs.push_back("-lcrt1.o");
1577 else if (isMacosxVersionLT(MacosxVersion, 10, 6))
1578 CmdArgs.push_back("-lcrt1.10.5.o");
1580 CmdArgs.push_back("-lcrt1.10.6.o");
1582 // darwin_crt2 spec is empty.
1588 if (Args.hasArg(options::OPT_shared_libgcc) &&
1589 !Args.hasArg(options::OPT_miphoneos_version_min_EQ) &&
1590 isMacosxVersionLT(MacosxVersion, 10, 5)) {
1591 const char *Str = getToolChain().GetFilePath(C, "crt3.o").c_str();
1592 CmdArgs.push_back(Args.MakeArgString(Str));
1596 Args.AddAllArgs(CmdArgs, options::OPT_L);
1598 if (Args.hasArg(options::OPT_fopenmp))
1599 // This is more complicated in gcc...
1600 CmdArgs.push_back("-lgomp");
1602 // FIXME: Derive these correctly.
1603 const char *TCDir = getDarwinToolChain().getToolChainDir().c_str();
1604 if (getToolChain().getArchName() == "x86_64") {
1605 CmdArgs.push_back(MakeFormattedString(Args,
1606 llvm::format("-L/usr/lib/gcc/%s/x86_64", TCDir)));
1607 // Intentionally duplicated for (temporary) gcc bug compatibility.
1608 CmdArgs.push_back(MakeFormattedString(Args,
1609 llvm::format("-L/usr/lib/gcc/%s/x86_64", TCDir)));
1611 CmdArgs.push_back(MakeFormattedString(Args,
1612 llvm::format("-L/usr/lib/%s", TCDir)));
1613 CmdArgs.push_back(MakeFormattedString(Args,
1614 llvm::format("-L/usr/lib/gcc/%s", TCDir)));
1615 // Intentionally duplicated for (temporary) gcc bug compatibility.
1616 CmdArgs.push_back(MakeFormattedString(Args,
1617 llvm::format("-L/usr/lib/gcc/%s", TCDir)));
1618 CmdArgs.push_back(MakeFormattedString(Args,
1619 llvm::format("-L/usr/lib/gcc/%s/../../../%s", TCDir, TCDir)));
1620 CmdArgs.push_back(MakeFormattedString(Args,
1621 llvm::format("-L/usr/lib/gcc/%s/../../..", TCDir)));
1623 for (InputInfoList::const_iterator
1624 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
1625 const InputInfo &II = *it;
1626 if (II.isFilename())
1627 CmdArgs.push_back(II.getFilename());
1629 II.getInputArg().renderAsInput(Args, CmdArgs);
1632 if (LinkingOutput) {
1633 CmdArgs.push_back("-arch_multiple");
1634 CmdArgs.push_back("-final_output");
1635 CmdArgs.push_back(LinkingOutput);
1638 if (Args.hasArg(options::OPT_fprofile_arcs) ||
1639 Args.hasArg(options::OPT_fprofile_generate) ||
1640 Args.hasArg(options::OPT_fcreate_profile) ||
1641 Args.hasArg(options::OPT_coverage))
1642 CmdArgs.push_back("-lgcov");
1644 if (Args.hasArg(options::OPT_fnested_functions))
1645 CmdArgs.push_back("-allow_stack_execute");
1647 if (!Args.hasArg(options::OPT_nostdlib) &&
1648 !Args.hasArg(options::OPT_nodefaultlibs)) {
1649 // FIXME: g++ is more complicated here, it tries to put -lstdc++
1650 // before -lm, for example.
1651 if (getToolChain().getHost().getDriver().CCCIsCXX)
1652 CmdArgs.push_back("-lstdc++");
1654 // link_ssp spec is empty.
1656 // Derived from libgcc and lib specs but refactored.
1657 if (Args.hasArg(options::OPT_static)) {
1658 CmdArgs.push_back("-lgcc_static");
1660 if (Args.hasArg(options::OPT_static_libgcc)) {
1661 CmdArgs.push_back("-lgcc_eh");
1662 } else if (Args.hasArg(options::OPT_miphoneos_version_min_EQ)) {
1663 // Derived from darwin_iphoneos_libgcc spec.
1664 CmdArgs.push_back("-lgcc_s.10.5");
1665 } else if (Args.hasArg(options::OPT_shared_libgcc) ||
1666 Args.hasArg(options::OPT_fexceptions) ||
1667 Args.hasArg(options::OPT_fgnu_runtime)) {
1668 // FIXME: This is probably broken on 10.3?
1669 if (isMacosxVersionLT(MacosxVersion, 10, 5))
1670 CmdArgs.push_back("-lgcc_s.10.4");
1671 else if (isMacosxVersionLT(MacosxVersion, 10, 6))
1672 CmdArgs.push_back("-lgcc_s.10.5");
1674 if (isMacosxVersionLT(MacosxVersion, 10, 3, 9))
1676 else if (isMacosxVersionLT(MacosxVersion, 10, 5))
1677 CmdArgs.push_back("-lgcc_s.10.4");
1678 else if (isMacosxVersionLT(MacosxVersion, 10, 6))
1679 CmdArgs.push_back("-lgcc_s.10.5");
1682 if (isMacosxVersionLT(MacosxVersion, 10, 6)) {
1683 CmdArgs.push_back("-lgcc");
1684 CmdArgs.push_back("-lSystem");
1686 CmdArgs.push_back("-lSystem");
1687 CmdArgs.push_back("-lgcc");
1692 if (!Args.hasArg(options::OPT_A) &&
1693 !Args.hasArg(options::OPT_nostdlib) &&
1694 !Args.hasArg(options::OPT_nostartfiles)) {
1695 // endfile_spec is empty.
1698 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
1699 Args.AddAllArgs(CmdArgs, options::OPT_F);
1702 Args.MakeArgString(getToolChain().GetProgramPath(C, "ld").c_str());
1703 Dest.addCommand(new Command(JA, Exec, CmdArgs));
1705 // Find the first non-empty base input (we want to ignore linker
1707 const char *BaseInput = "";
1708 for (unsigned i = 0, e = Inputs.size(); i != e; ++i) {
1709 if (Inputs[i].getBaseInput()[0] != '\0') {
1710 BaseInput = Inputs[i].getBaseInput();
1715 // Run dsymutil if we are making an executable in a single step.
1717 // FIXME: Currently we don't want to do this when we are part of a
1718 // universal build step, as this would end up creating stray temp
1720 if (!LinkingOutput &&
1721 Args.getLastArg(options::OPT_g_Group) &&
1722 !Args.getLastArg(options::OPT_gstabs) &&
1723 !Args.getLastArg(options::OPT_g0)) {
1724 // FIXME: This is gross, but matches gcc. The test only considers
1725 // the suffix (not the -x type), and then only of the first
1726 // source input. Awesome.
1727 const char *Suffix = strrchr(BaseInput, '.');
1728 if (Suffix && isSourceSuffix(Suffix + 1)) {
1730 Args.MakeArgString(getToolChain().GetProgramPath(C, "dsymutil").c_str());
1731 ArgStringList CmdArgs;
1732 CmdArgs.push_back(Output.getFilename());
1733 C.getJobs().addCommand(new Command(JA, Exec, CmdArgs));
1738 void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA,
1739 Job &Dest, const InputInfo &Output,
1740 const InputInfoList &Inputs,
1741 const ArgList &Args,
1742 const char *LinkingOutput) const {
1743 ArgStringList CmdArgs;
1745 CmdArgs.push_back("-create");
1746 assert(Output.isFilename() && "Unexpected lipo output.");
1748 CmdArgs.push_back("-output");
1749 CmdArgs.push_back(Output.getFilename());
1751 for (InputInfoList::const_iterator
1752 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
1753 const InputInfo &II = *it;
1754 assert(II.isFilename() && "Unexpected lipo input.");
1755 CmdArgs.push_back(II.getFilename());
1758 Args.MakeArgString(getToolChain().GetProgramPath(C, "lipo").c_str());
1759 Dest.addCommand(new Command(JA, Exec, CmdArgs));
1762 void openbsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
1763 Job &Dest, const InputInfo &Output,
1764 const InputInfoList &Inputs,
1765 const ArgList &Args,
1766 const char *LinkingOutput) const
1768 ArgStringList CmdArgs;
1770 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
1771 options::OPT_Xassembler);
1773 CmdArgs.push_back("-o");
1774 if (Output.isPipe())
1775 CmdArgs.push_back("-");
1777 CmdArgs.push_back(Output.getFilename());
1779 for (InputInfoList::const_iterator
1780 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
1781 const InputInfo &II = *it;
1783 CmdArgs.push_back("-");
1785 CmdArgs.push_back(II.getFilename());
1789 Args.MakeArgString(getToolChain().GetProgramPath(C, "as").c_str());
1790 Dest.addCommand(new Command(JA, Exec, CmdArgs));
1793 void openbsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
1794 Job &Dest, const InputInfo &Output,
1795 const InputInfoList &Inputs,
1796 const ArgList &Args,
1797 const char *LinkingOutput) const {
1798 const Driver &D = getToolChain().getHost().getDriver();
1799 ArgStringList CmdArgs;
1801 if (Args.hasArg(options::OPT_static)) {
1802 CmdArgs.push_back("-Bstatic");
1804 CmdArgs.push_back("--eh-frame-hdr");
1805 if (Args.hasArg(options::OPT_shared)) {
1806 CmdArgs.push_back("-Bshareable");
1808 CmdArgs.push_back("-dynamic-linker");
1809 CmdArgs.push_back("/usr/libexec/ld.so");
1813 if (Output.isPipe()) {
1814 CmdArgs.push_back("-o");
1815 CmdArgs.push_back("-");
1816 } else if (Output.isFilename()) {
1817 CmdArgs.push_back("-o");
1818 CmdArgs.push_back(Output.getFilename());
1820 assert(Output.isNothing() && "Invalid output.");
1823 if (!Args.hasArg(options::OPT_nostdlib) &&
1824 !Args.hasArg(options::OPT_nostartfiles)) {
1825 if (!Args.hasArg(options::OPT_shared)) {
1826 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crt0.o").c_str()));
1827 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtbegin.o").c_str()));
1829 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtbeginS.o").c_str()));
1833 Args.AddAllArgs(CmdArgs, options::OPT_L);
1834 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
1835 Args.AddAllArgs(CmdArgs, options::OPT_e);
1837 for (InputInfoList::const_iterator
1838 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
1839 const InputInfo &II = *it;
1841 // Don't try to pass LLVM inputs to a generic gcc.
1842 if (II.getType() == types::TY_LLVMBC)
1843 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
1844 << getToolChain().getTripleString().c_str();
1847 CmdArgs.push_back("-");
1848 else if (II.isFilename())
1849 CmdArgs.push_back(II.getFilename());
1851 II.getInputArg().renderAsInput(Args, CmdArgs);
1854 if (!Args.hasArg(options::OPT_nostdlib) &&
1855 !Args.hasArg(options::OPT_nodefaultlibs)) {
1857 if (Args.hasArg(options::OPT_pthread))
1858 CmdArgs.push_back("-pthread");
1859 CmdArgs.push_back("-lc");
1862 if (!Args.hasArg(options::OPT_nostdlib) &&
1863 !Args.hasArg(options::OPT_nostartfiles)) {
1864 if (!Args.hasArg(options::OPT_shared))
1865 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtend.o").c_str()));
1867 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtendS.o").c_str()));
1871 Args.MakeArgString(getToolChain().GetProgramPath(C, "ld").c_str());
1872 Dest.addCommand(new Command(JA, Exec, CmdArgs));
1875 void freebsd::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
1876 Job &Dest, const InputInfo &Output,
1877 const InputInfoList &Inputs,
1878 const ArgList &Args,
1879 const char *LinkingOutput) const
1881 ArgStringList CmdArgs;
1883 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
1884 // instruct as in the base system to assemble 32-bit code.
1885 if (getToolChain().getArchName() == "i386")
1886 CmdArgs.push_back("--32");
1888 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
1889 options::OPT_Xassembler);
1891 CmdArgs.push_back("-o");
1892 if (Output.isPipe())
1893 CmdArgs.push_back("-");
1895 CmdArgs.push_back(Output.getFilename());
1897 for (InputInfoList::const_iterator
1898 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
1899 const InputInfo &II = *it;
1901 CmdArgs.push_back("-");
1903 CmdArgs.push_back(II.getFilename());
1907 Args.MakeArgString(getToolChain().GetProgramPath(C, "as").c_str());
1908 Dest.addCommand(new Command(JA, Exec, CmdArgs));
1911 void freebsd::Link::ConstructJob(Compilation &C, const JobAction &JA,
1912 Job &Dest, const InputInfo &Output,
1913 const InputInfoList &Inputs,
1914 const ArgList &Args,
1915 const char *LinkingOutput) const {
1916 const Driver &D = getToolChain().getHost().getDriver();
1917 ArgStringList CmdArgs;
1919 if (Args.hasArg(options::OPT_static)) {
1920 CmdArgs.push_back("-Bstatic");
1922 CmdArgs.push_back("--eh-frame-hdr");
1923 if (Args.hasArg(options::OPT_shared)) {
1924 CmdArgs.push_back("-Bshareable");
1926 CmdArgs.push_back("-dynamic-linker");
1927 CmdArgs.push_back("/libexec/ld-elf.so.1");
1931 // When building 32-bit code on FreeBSD/amd64, we have to explicitly
1932 // instruct ld in the base system to link 32-bit code.
1933 if (getToolChain().getArchName() == "i386") {
1934 CmdArgs.push_back("-m");
1935 CmdArgs.push_back("elf_i386_fbsd");
1938 if (Output.isPipe()) {
1939 CmdArgs.push_back("-o");
1940 CmdArgs.push_back("-");
1941 } else if (Output.isFilename()) {
1942 CmdArgs.push_back("-o");
1943 CmdArgs.push_back(Output.getFilename());
1945 assert(Output.isNothing() && "Invalid output.");
1948 if (!Args.hasArg(options::OPT_nostdlib) &&
1949 !Args.hasArg(options::OPT_nostartfiles)) {
1950 if (!Args.hasArg(options::OPT_shared)) {
1951 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crt1.o").c_str()));
1952 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crti.o").c_str()));
1953 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtbegin.o").c_str()));
1955 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crti.o").c_str()));
1956 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtbeginS.o").c_str()));
1960 Args.AddAllArgs(CmdArgs, options::OPT_L);
1961 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
1962 Args.AddAllArgs(CmdArgs, options::OPT_e);
1964 for (InputInfoList::const_iterator
1965 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
1966 const InputInfo &II = *it;
1968 // Don't try to pass LLVM inputs to a generic gcc.
1969 if (II.getType() == types::TY_LLVMBC)
1970 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
1971 << getToolChain().getTripleString().c_str();
1974 CmdArgs.push_back("-");
1975 else if (II.isFilename())
1976 CmdArgs.push_back(II.getFilename());
1978 II.getInputArg().renderAsInput(Args, CmdArgs);
1981 if (!Args.hasArg(options::OPT_nostdlib) &&
1982 !Args.hasArg(options::OPT_nodefaultlibs)) {
1983 // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
1984 // the default system libraries. Just mimic this for now.
1985 CmdArgs.push_back("-lgcc");
1986 if (Args.hasArg(options::OPT_static)) {
1987 CmdArgs.push_back("-lgcc_eh");
1989 CmdArgs.push_back("--as-needed");
1990 CmdArgs.push_back("-lgcc_s");
1991 CmdArgs.push_back("--no-as-needed");
1994 if (Args.hasArg(options::OPT_pthread))
1995 CmdArgs.push_back("-lpthread");
1996 CmdArgs.push_back("-lc");
1998 CmdArgs.push_back("-lgcc");
1999 if (Args.hasArg(options::OPT_static)) {
2000 CmdArgs.push_back("-lgcc_eh");
2002 CmdArgs.push_back("--as-needed");
2003 CmdArgs.push_back("-lgcc_s");
2004 CmdArgs.push_back("--no-as-needed");
2008 if (!Args.hasArg(options::OPT_nostdlib) &&
2009 !Args.hasArg(options::OPT_nostartfiles)) {
2010 if (!Args.hasArg(options::OPT_shared))
2011 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtend.o").c_str()));
2013 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtendS.o").c_str()));
2014 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtn.o").c_str()));
2018 Args.MakeArgString(getToolChain().GetProgramPath(C, "ld").c_str());
2019 Dest.addCommand(new Command(JA, Exec, CmdArgs));
2024 // For now, DragonFly Assemble does just about the same as for
2025 // FreeBSD, but this may change soon.
2026 void dragonfly::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
2027 Job &Dest, const InputInfo &Output,
2028 const InputInfoList &Inputs,
2029 const ArgList &Args,
2030 const char *LinkingOutput) const {
2031 ArgStringList CmdArgs;
2033 // When building 32-bit code on DragonFly/pc64, we have to explicitly
2034 // instruct as in the base system to assemble 32-bit code.
2035 if (getToolChain().getArchName() == "i386")
2036 CmdArgs.push_back("--32");
2038 Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA,
2039 options::OPT_Xassembler);
2041 CmdArgs.push_back("-o");
2042 if (Output.isPipe())
2043 CmdArgs.push_back("-");
2045 CmdArgs.push_back(Output.getFilename());
2047 for (InputInfoList::const_iterator
2048 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2049 const InputInfo &II = *it;
2051 CmdArgs.push_back("-");
2053 CmdArgs.push_back(II.getFilename());
2057 Args.MakeArgString(getToolChain().GetProgramPath(C, "as").c_str());
2058 Dest.addCommand(new Command(JA, Exec, CmdArgs));
2061 void dragonfly::Link::ConstructJob(Compilation &C, const JobAction &JA,
2062 Job &Dest, const InputInfo &Output,
2063 const InputInfoList &Inputs,
2064 const ArgList &Args,
2065 const char *LinkingOutput) const {
2066 const Driver &D = getToolChain().getHost().getDriver();
2067 ArgStringList CmdArgs;
2069 if (Args.hasArg(options::OPT_static)) {
2070 CmdArgs.push_back("-Bstatic");
2072 if (Args.hasArg(options::OPT_shared))
2073 CmdArgs.push_back("-Bshareable");
2075 CmdArgs.push_back("-dynamic-linker");
2076 CmdArgs.push_back("/usr/libexec/ld-elf.so.2");
2080 // When building 32-bit code on DragonFly/pc64, we have to explicitly
2081 // instruct ld in the base system to link 32-bit code.
2082 if (getToolChain().getArchName() == "i386") {
2083 CmdArgs.push_back("-m");
2084 CmdArgs.push_back("elf_i386");
2087 if (Output.isPipe()) {
2088 CmdArgs.push_back("-o");
2089 CmdArgs.push_back("-");
2090 } else if (Output.isFilename()) {
2091 CmdArgs.push_back("-o");
2092 CmdArgs.push_back(Output.getFilename());
2094 assert(Output.isNothing() && "Invalid output.");
2097 if (!Args.hasArg(options::OPT_nostdlib) &&
2098 !Args.hasArg(options::OPT_nostartfiles)) {
2099 if (!Args.hasArg(options::OPT_shared)) {
2100 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crt1.o").c_str()));
2101 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crti.o").c_str()));
2102 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtbegin.o").c_str()));
2104 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crti.o").c_str()));
2105 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtbeginS.o").c_str()));
2109 Args.AddAllArgs(CmdArgs, options::OPT_L);
2110 Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
2111 Args.AddAllArgs(CmdArgs, options::OPT_e);
2113 for (InputInfoList::const_iterator
2114 it = Inputs.begin(), ie = Inputs.end(); it != ie; ++it) {
2115 const InputInfo &II = *it;
2117 // Don't try to pass LLVM inputs to a generic gcc.
2118 if (II.getType() == types::TY_LLVMBC)
2119 D.Diag(clang::diag::err_drv_no_linker_llvm_support)
2120 << getToolChain().getTripleString().c_str();
2123 CmdArgs.push_back("-");
2124 else if (II.isFilename())
2125 CmdArgs.push_back(II.getFilename());
2127 II.getInputArg().renderAsInput(Args, CmdArgs);
2130 if (!Args.hasArg(options::OPT_nostdlib) &&
2131 !Args.hasArg(options::OPT_nodefaultlibs)) {
2132 // FIXME: GCC passes on -lgcc, -lgcc_pic and a whole lot of
2134 CmdArgs.push_back("-L/usr/lib/gcc41");
2136 if (!Args.hasArg(options::OPT_static)) {
2137 CmdArgs.push_back("-rpath");
2138 CmdArgs.push_back("/usr/lib/gcc41");
2140 CmdArgs.push_back("-rpath-link");
2141 CmdArgs.push_back("/usr/lib/gcc41");
2143 CmdArgs.push_back("-rpath");
2144 CmdArgs.push_back("/usr/lib");
2146 CmdArgs.push_back("-rpath-link");
2147 CmdArgs.push_back("/usr/lib");
2150 if (Args.hasArg(options::OPT_shared)) {
2151 CmdArgs.push_back("-lgcc_pic");
2153 CmdArgs.push_back("-lgcc");
2157 if (Args.hasArg(options::OPT_pthread))
2158 CmdArgs.push_back("-lthread_xu");
2160 if (!Args.hasArg(options::OPT_nolibc)) {
2161 CmdArgs.push_back("-lc");
2164 if (Args.hasArg(options::OPT_shared)) {
2165 CmdArgs.push_back("-lgcc_pic");
2167 CmdArgs.push_back("-lgcc");
2171 if (!Args.hasArg(options::OPT_nostdlib) &&
2172 !Args.hasArg(options::OPT_nostartfiles)) {
2173 if (!Args.hasArg(options::OPT_shared))
2174 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtend.o").c_str()));
2176 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtendS.o").c_str()));
2177 CmdArgs.push_back(Args.MakeArgString(getToolChain().GetFilePath(C, "crtn.o").c_str()));
2181 Args.MakeArgString(getToolChain().GetProgramPath(C, "ld").c_str());
2182 Dest.addCommand(new Command(JA, Exec, CmdArgs));