clang  6.0.0svn
Darwin.cpp
Go to the documentation of this file.
1 //===--- Darwin.cpp - Darwin Tool and ToolChain Implementations -*- C++ -*-===//
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 "Darwin.h"
11 #include "Arch/ARM.h"
12 #include "CommonArgs.h"
17 #include "clang/Driver/Driver.h"
19 #include "clang/Driver/Options.h"
21 #include "llvm/ADT/StringSwitch.h"
22 #include "llvm/Option/ArgList.h"
23 #include "llvm/Support/Path.h"
24 #include "llvm/Support/ScopedPrinter.h"
25 #include "llvm/Support/TargetParser.h"
26 #include <cstdlib> // ::getenv
27 
28 using namespace clang::driver;
29 using namespace clang::driver::tools;
30 using namespace clang::driver::toolchains;
31 using namespace clang;
32 using namespace llvm::opt;
33 
34 llvm::Triple::ArchType darwin::getArchTypeForMachOArchName(StringRef Str) {
35  // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
36  // archs which Darwin doesn't use.
37 
38  // The matching this routine does is fairly pointless, since it is neither the
39  // complete architecture list, nor a reasonable subset. The problem is that
40  // historically the driver driver accepts this and also ties its -march=
41  // handling to the architecture name, so we need to be careful before removing
42  // support for it.
43 
44  // This code must be kept in sync with Clang's Darwin specific argument
45  // translation.
46 
47  return llvm::StringSwitch<llvm::Triple::ArchType>(Str)
48  .Cases("ppc", "ppc601", "ppc603", "ppc604", "ppc604e", llvm::Triple::ppc)
49  .Cases("ppc750", "ppc7400", "ppc7450", "ppc970", llvm::Triple::ppc)
50  .Case("ppc64", llvm::Triple::ppc64)
51  .Cases("i386", "i486", "i486SX", "i586", "i686", llvm::Triple::x86)
52  .Cases("pentium", "pentpro", "pentIIm3", "pentIIm5", "pentium4",
53  llvm::Triple::x86)
54  .Cases("x86_64", "x86_64h", llvm::Triple::x86_64)
55  // This is derived from the driver driver.
56  .Cases("arm", "armv4t", "armv5", "armv6", "armv6m", llvm::Triple::arm)
57  .Cases("armv7", "armv7em", "armv7k", "armv7m", llvm::Triple::arm)
58  .Cases("armv7s", "xscale", llvm::Triple::arm)
59  .Case("arm64", llvm::Triple::aarch64)
60  .Case("r600", llvm::Triple::r600)
61  .Case("amdgcn", llvm::Triple::amdgcn)
62  .Case("nvptx", llvm::Triple::nvptx)
63  .Case("nvptx64", llvm::Triple::nvptx64)
64  .Case("amdil", llvm::Triple::amdil)
65  .Case("spir", llvm::Triple::spir)
66  .Default(llvm::Triple::UnknownArch);
67 }
68 
69 void darwin::setTripleTypeForMachOArchName(llvm::Triple &T, StringRef Str) {
70  const llvm::Triple::ArchType Arch = getArchTypeForMachOArchName(Str);
71  llvm::ARM::ArchKind ArchKind = llvm::ARM::parseArch(Str);
72  T.setArch(Arch);
73 
74  if (Str == "x86_64h")
75  T.setArchName(Str);
76  else if (ArchKind == llvm::ARM::ArchKind::ARMV6M ||
77  ArchKind == llvm::ARM::ArchKind::ARMV7M ||
78  ArchKind == llvm::ARM::ArchKind::ARMV7EM) {
79  T.setOS(llvm::Triple::UnknownOS);
80  T.setObjectFormat(llvm::Triple::MachO);
81  }
82 }
83 
85  const InputInfo &Output,
86  const InputInfoList &Inputs,
87  const ArgList &Args,
88  const char *LinkingOutput) const {
89  ArgStringList CmdArgs;
90 
91  assert(Inputs.size() == 1 && "Unexpected number of inputs.");
92  const InputInfo &Input = Inputs[0];
93 
94  // Determine the original source input.
95  const Action *SourceAction = &JA;
96  while (SourceAction->getKind() != Action::InputClass) {
97  assert(!SourceAction->getInputs().empty() && "unexpected root action!");
98  SourceAction = SourceAction->getInputs()[0];
99  }
100 
101  // If -fno-integrated-as is used add -Q to the darwin assember driver to make
102  // sure it runs its system assembler not clang's integrated assembler.
103  // Applicable to darwin11+ and Xcode 4+. darwin<10 lacked integrated-as.
104  // FIXME: at run-time detect assembler capabilities or rely on version
105  // information forwarded by -target-assembler-version.
106  if (Args.hasArg(options::OPT_fno_integrated_as)) {
107  const llvm::Triple &T(getToolChain().getTriple());
108  if (!(T.isMacOSX() && T.isMacOSXVersionLT(10, 7)))
109  CmdArgs.push_back("-Q");
110  }
111 
112  // Forward -g, assuming we are dealing with an actual assembly file.
113  if (SourceAction->getType() == types::TY_Asm ||
114  SourceAction->getType() == types::TY_PP_Asm) {
115  if (Args.hasArg(options::OPT_gstabs))
116  CmdArgs.push_back("--gstabs");
117  else if (Args.hasArg(options::OPT_g_Group))
118  CmdArgs.push_back("-g");
119  }
120 
121  // Derived from asm spec.
122  AddMachOArch(Args, CmdArgs);
123 
124  // Use -force_cpusubtype_ALL on x86 by default.
125  if (getToolChain().getArch() == llvm::Triple::x86 ||
126  getToolChain().getArch() == llvm::Triple::x86_64 ||
127  Args.hasArg(options::OPT_force__cpusubtype__ALL))
128  CmdArgs.push_back("-force_cpusubtype_ALL");
129 
130  if (getToolChain().getArch() != llvm::Triple::x86_64 &&
131  (((Args.hasArg(options::OPT_mkernel) ||
132  Args.hasArg(options::OPT_fapple_kext)) &&
133  getMachOToolChain().isKernelStatic()) ||
134  Args.hasArg(options::OPT_static)))
135  CmdArgs.push_back("-static");
136 
137  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
138 
139  assert(Output.isFilename() && "Unexpected lipo output.");
140  CmdArgs.push_back("-o");
141  CmdArgs.push_back(Output.getFilename());
142 
143  assert(Input.isFilename() && "Invalid input.");
144  CmdArgs.push_back(Input.getFilename());
145 
146  // asm_final spec is empty.
147 
148  const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
149  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
150 }
151 
152 void darwin::MachOTool::anchor() {}
153 
154 void darwin::MachOTool::AddMachOArch(const ArgList &Args,
155  ArgStringList &CmdArgs) const {
156  StringRef ArchName = getMachOToolChain().getMachOArchName(Args);
157 
158  // Derived from darwin_arch spec.
159  CmdArgs.push_back("-arch");
160  CmdArgs.push_back(Args.MakeArgString(ArchName));
161 
162  // FIXME: Is this needed anymore?
163  if (ArchName == "arm")
164  CmdArgs.push_back("-force_cpusubtype_ALL");
165 }
166 
167 bool darwin::Linker::NeedsTempPath(const InputInfoList &Inputs) const {
168  // We only need to generate a temp path for LTO if we aren't compiling object
169  // files. When compiling source files, we run 'dsymutil' after linking. We
170  // don't run 'dsymutil' when compiling object files.
171  for (const auto &Input : Inputs)
172  if (Input.getType() != types::TY_Object)
173  return true;
174 
175  return false;
176 }
177 
178 /// \brief Pass -no_deduplicate to ld64 under certain conditions:
179 ///
180 /// - Either -O0 or -O1 is explicitly specified
181 /// - No -O option is specified *and* this is a compile+link (implicit -O0)
182 ///
183 /// Also do *not* add -no_deduplicate when no -O option is specified and this
184 /// is just a link (we can't imply -O0)
185 static bool shouldLinkerNotDedup(bool IsLinkerOnlyAction, const ArgList &Args) {
186  if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
187  if (A->getOption().matches(options::OPT_O0))
188  return true;
189  if (A->getOption().matches(options::OPT_O))
190  return llvm::StringSwitch<bool>(A->getValue())
191  .Case("1", true)
192  .Default(false);
193  return false; // OPT_Ofast & OPT_O4
194  }
195 
196  if (!IsLinkerOnlyAction) // Implicit -O0 for compile+linker only.
197  return true;
198  return false;
199 }
200 
201 void darwin::Linker::AddLinkArgs(Compilation &C, const ArgList &Args,
202  ArgStringList &CmdArgs,
203  const InputInfoList &Inputs) const {
204  const Driver &D = getToolChain().getDriver();
205  const toolchains::MachO &MachOTC = getMachOToolChain();
206 
207  unsigned Version[5] = {0, 0, 0, 0, 0};
208  if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
209  if (!Driver::GetReleaseVersion(A->getValue(), Version))
210  D.Diag(diag::err_drv_invalid_version_number) << A->getAsString(Args);
211  }
212 
213  // Newer linkers support -demangle. Pass it if supported and not disabled by
214  // the user.
215  if (Version[0] >= 100 && !Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
216  CmdArgs.push_back("-demangle");
217 
218  if (Args.hasArg(options::OPT_rdynamic) && Version[0] >= 137)
219  CmdArgs.push_back("-export_dynamic");
220 
221  // If we are using App Extension restrictions, pass a flag to the linker
222  // telling it that the compiled code has been audited.
223  if (Args.hasFlag(options::OPT_fapplication_extension,
224  options::OPT_fno_application_extension, false))
225  CmdArgs.push_back("-application_extension");
226 
227  if (D.isUsingLTO()) {
228  // If we are using LTO, then automatically create a temporary file path for
229  // the linker to use, so that it's lifetime will extend past a possible
230  // dsymutil step.
231  if (Version[0] >= 116 && NeedsTempPath(Inputs)) {
232  const char *TmpPath = C.getArgs().MakeArgString(
233  D.GetTemporaryPath("cc", types::getTypeTempSuffix(types::TY_Object)));
234  C.addTempFile(TmpPath);
235  CmdArgs.push_back("-object_path_lto");
236  CmdArgs.push_back(TmpPath);
237  }
238  }
239 
240  // Use -lto_library option to specify the libLTO.dylib path. Try to find
241  // it in clang installed libraries. ld64 will only look at this argument
242  // when it actually uses LTO, so libLTO.dylib only needs to exist at link
243  // time if ld64 decides that it needs to use LTO.
244  // Since this is passed unconditionally, ld64 will never look for libLTO.dylib
245  // next to it. That's ok since ld64 using a libLTO.dylib not matching the
246  // clang version won't work anyways.
247  if (Version[0] >= 133) {
248  // Search for libLTO in <InstalledDir>/../lib/libLTO.dylib
249  StringRef P = llvm::sys::path::parent_path(D.Dir);
250  SmallString<128> LibLTOPath(P);
251  llvm::sys::path::append(LibLTOPath, "lib");
252  llvm::sys::path::append(LibLTOPath, "libLTO.dylib");
253  CmdArgs.push_back("-lto_library");
254  CmdArgs.push_back(C.getArgs().MakeArgString(LibLTOPath));
255  }
256 
257  // ld64 version 262 and above run the deduplicate pass by default.
258  if (Version[0] >= 262 && shouldLinkerNotDedup(C.getJobs().empty(), Args))
259  CmdArgs.push_back("-no_deduplicate");
260 
261  // Derived from the "link" spec.
262  Args.AddAllArgs(CmdArgs, options::OPT_static);
263  if (!Args.hasArg(options::OPT_static))
264  CmdArgs.push_back("-dynamic");
265  if (Args.hasArg(options::OPT_fgnu_runtime)) {
266  // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu
267  // here. How do we wish to handle such things?
268  }
269 
270  if (!Args.hasArg(options::OPT_dynamiclib)) {
271  AddMachOArch(Args, CmdArgs);
272  // FIXME: Why do this only on this path?
273  Args.AddLastArg(CmdArgs, options::OPT_force__cpusubtype__ALL);
274 
275  Args.AddLastArg(CmdArgs, options::OPT_bundle);
276  Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader);
277  Args.AddAllArgs(CmdArgs, options::OPT_client__name);
278 
279  Arg *A;
280  if ((A = Args.getLastArg(options::OPT_compatibility__version)) ||
281  (A = Args.getLastArg(options::OPT_current__version)) ||
282  (A = Args.getLastArg(options::OPT_install__name)))
283  D.Diag(diag::err_drv_argument_only_allowed_with) << A->getAsString(Args)
284  << "-dynamiclib";
285 
286  Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace);
287  Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs);
288  Args.AddLastArg(CmdArgs, options::OPT_private__bundle);
289  } else {
290  CmdArgs.push_back("-dylib");
291 
292  Arg *A;
293  if ((A = Args.getLastArg(options::OPT_bundle)) ||
294  (A = Args.getLastArg(options::OPT_bundle__loader)) ||
295  (A = Args.getLastArg(options::OPT_client__name)) ||
296  (A = Args.getLastArg(options::OPT_force__flat__namespace)) ||
297  (A = Args.getLastArg(options::OPT_keep__private__externs)) ||
298  (A = Args.getLastArg(options::OPT_private__bundle)))
299  D.Diag(diag::err_drv_argument_not_allowed_with) << A->getAsString(Args)
300  << "-dynamiclib";
301 
302  Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version,
303  "-dylib_compatibility_version");
304  Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version,
305  "-dylib_current_version");
306 
307  AddMachOArch(Args, CmdArgs);
308 
309  Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name,
310  "-dylib_install_name");
311  }
312 
313  Args.AddLastArg(CmdArgs, options::OPT_all__load);
314  Args.AddAllArgs(CmdArgs, options::OPT_allowable__client);
315  Args.AddLastArg(CmdArgs, options::OPT_bind__at__load);
316  if (MachOTC.isTargetIOSBased())
317  Args.AddLastArg(CmdArgs, options::OPT_arch__errors__fatal);
318  Args.AddLastArg(CmdArgs, options::OPT_dead__strip);
319  Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms);
320  Args.AddAllArgs(CmdArgs, options::OPT_dylib__file);
321  Args.AddLastArg(CmdArgs, options::OPT_dynamic);
322  Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list);
323  Args.AddLastArg(CmdArgs, options::OPT_flat__namespace);
324  Args.AddAllArgs(CmdArgs, options::OPT_force__load);
325  Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names);
326  Args.AddAllArgs(CmdArgs, options::OPT_image__base);
327  Args.AddAllArgs(CmdArgs, options::OPT_init);
328 
329  // Add the deployment target.
330  MachOTC.addMinVersionArgs(Args, CmdArgs);
331 
332  Args.AddLastArg(CmdArgs, options::OPT_nomultidefs);
333  Args.AddLastArg(CmdArgs, options::OPT_multi__module);
334  Args.AddLastArg(CmdArgs, options::OPT_single__module);
335  Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined);
336  Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused);
337 
338  if (const Arg *A =
339  Args.getLastArg(options::OPT_fpie, options::OPT_fPIE,
340  options::OPT_fno_pie, options::OPT_fno_PIE)) {
341  if (A->getOption().matches(options::OPT_fpie) ||
342  A->getOption().matches(options::OPT_fPIE))
343  CmdArgs.push_back("-pie");
344  else
345  CmdArgs.push_back("-no_pie");
346  }
347 
348  // for embed-bitcode, use -bitcode_bundle in linker command
349  if (C.getDriver().embedBitcodeEnabled()) {
350  // Check if the toolchain supports bitcode build flow.
351  if (MachOTC.SupportsEmbeddedBitcode()) {
352  CmdArgs.push_back("-bitcode_bundle");
353  if (C.getDriver().embedBitcodeMarkerOnly() && Version[0] >= 278) {
354  CmdArgs.push_back("-bitcode_process_mode");
355  CmdArgs.push_back("marker");
356  }
357  } else
358  D.Diag(diag::err_drv_bitcode_unsupported_on_toolchain);
359  }
360 
361  Args.AddLastArg(CmdArgs, options::OPT_prebind);
362  Args.AddLastArg(CmdArgs, options::OPT_noprebind);
363  Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding);
364  Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules);
365  Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs);
366  Args.AddAllArgs(CmdArgs, options::OPT_sectcreate);
367  Args.AddAllArgs(CmdArgs, options::OPT_sectorder);
368  Args.AddAllArgs(CmdArgs, options::OPT_seg1addr);
369  Args.AddAllArgs(CmdArgs, options::OPT_segprot);
370  Args.AddAllArgs(CmdArgs, options::OPT_segaddr);
371  Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr);
372  Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr);
373  Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table);
374  Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename);
375  Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
376  Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);
377 
378  // Give --sysroot= preference, over the Apple specific behavior to also use
379  // --isysroot as the syslibroot.
380  StringRef sysroot = C.getSysRoot();
381  if (sysroot != "") {
382  CmdArgs.push_back("-syslibroot");
383  CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
384  } else if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
385  CmdArgs.push_back("-syslibroot");
386  CmdArgs.push_back(A->getValue());
387  }
388 
389  Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);
390  Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints);
391  Args.AddAllArgs(CmdArgs, options::OPT_umbrella);
392  Args.AddAllArgs(CmdArgs, options::OPT_undefined);
393  Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list);
394  Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches);
395  Args.AddLastArg(CmdArgs, options::OPT_X_Flag);
396  Args.AddAllArgs(CmdArgs, options::OPT_y);
397  Args.AddLastArg(CmdArgs, options::OPT_w);
398  Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size);
399  Args.AddAllArgs(CmdArgs, options::OPT_segs__read__);
400  Args.AddLastArg(CmdArgs, options::OPT_seglinkedit);
401  Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit);
402  Args.AddAllArgs(CmdArgs, options::OPT_sectalign);
403  Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols);
404  Args.AddAllArgs(CmdArgs, options::OPT_segcreate);
405  Args.AddLastArg(CmdArgs, options::OPT_whyload);
406  Args.AddLastArg(CmdArgs, options::OPT_whatsloaded);
407  Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name);
408  Args.AddLastArg(CmdArgs, options::OPT_dylinker);
409  Args.AddLastArg(CmdArgs, options::OPT_Mach);
410 }
411 
412 /// \brief Determine whether we are linking the ObjC runtime.
413 static bool isObjCRuntimeLinked(const ArgList &Args) {
414  if (isObjCAutoRefCount(Args)) {
415  Args.ClaimAllArgs(options::OPT_fobjc_link_runtime);
416  return true;
417  }
418  return Args.hasArg(options::OPT_fobjc_link_runtime);
419 }
420 
422  const InputInfo &Output,
423  const InputInfoList &Inputs,
424  const ArgList &Args,
425  const char *LinkingOutput) const {
426  assert(Output.getType() == types::TY_Image && "Invalid linker output type.");
427 
428  // If the number of arguments surpasses the system limits, we will encode the
429  // input files in a separate file, shortening the command line. To this end,
430  // build a list of input file names that can be passed via a file with the
431  // -filelist linker option.
432  llvm::opt::ArgStringList InputFileList;
433 
434  // The logic here is derived from gcc's behavior; most of which
435  // comes from specs (starting with link_command). Consult gcc for
436  // more information.
437  ArgStringList CmdArgs;
438 
439  /// Hack(tm) to ignore linking errors when we are doing ARC migration.
440  if (Args.hasArg(options::OPT_ccc_arcmt_check,
441  options::OPT_ccc_arcmt_migrate)) {
442  for (const auto &Arg : Args)
443  Arg->claim();
444  const char *Exec =
445  Args.MakeArgString(getToolChain().GetProgramPath("touch"));
446  CmdArgs.push_back(Output.getFilename());
447  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, None));
448  return;
449  }
450 
451  // I'm not sure why this particular decomposition exists in gcc, but
452  // we follow suite for ease of comparison.
453  AddLinkArgs(C, Args, CmdArgs, Inputs);
454 
455  // For LTO, pass the name of the optimization record file.
456  if (Args.hasFlag(options::OPT_fsave_optimization_record,
457  options::OPT_fno_save_optimization_record, false)) {
458  CmdArgs.push_back("-mllvm");
459  CmdArgs.push_back("-lto-pass-remarks-output");
460  CmdArgs.push_back("-mllvm");
461 
463  F = Output.getFilename();
464  F += ".opt.yaml";
465  CmdArgs.push_back(Args.MakeArgString(F));
466 
467  if (getLastProfileUseArg(Args)) {
468  CmdArgs.push_back("-mllvm");
469  CmdArgs.push_back("-lto-pass-remarks-with-hotness");
470  }
471  }
472 
473  // It seems that the 'e' option is completely ignored for dynamic executables
474  // (the default), and with static executables, the last one wins, as expected.
475  Args.AddAllArgs(CmdArgs, {options::OPT_d_Flag, options::OPT_s, options::OPT_t,
476  options::OPT_Z_Flag, options::OPT_u_Group,
477  options::OPT_e, options::OPT_r});
478 
479  // Forward -ObjC when either -ObjC or -ObjC++ is used, to force loading
480  // members of static archive libraries which implement Objective-C classes or
481  // categories.
482  if (Args.hasArg(options::OPT_ObjC) || Args.hasArg(options::OPT_ObjCXX))
483  CmdArgs.push_back("-ObjC");
484 
485  CmdArgs.push_back("-o");
486  CmdArgs.push_back(Output.getFilename());
487 
488  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
489  getMachOToolChain().addStartObjectFileArgs(Args, CmdArgs);
490 
491  // SafeStack requires its own runtime libraries
492  // These libraries should be linked first, to make sure the
493  // __safestack_init constructor executes before everything else
494  if (getToolChain().getSanitizerArgs().needsSafeStackRt()) {
495  getMachOToolChain().AddLinkRuntimeLib(Args, CmdArgs,
496  "libclang_rt.safestack_osx.a",
498  }
499 
500  Args.AddAllArgs(CmdArgs, options::OPT_L);
501 
502  AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
503  // Build the input file for -filelist (list of linker input files) in case we
504  // need it later
505  for (const auto &II : Inputs) {
506  if (!II.isFilename()) {
507  // This is a linker input argument.
508  // We cannot mix input arguments and file names in a -filelist input, thus
509  // we prematurely stop our list (remaining files shall be passed as
510  // arguments).
511  if (InputFileList.size() > 0)
512  break;
513 
514  continue;
515  }
516 
517  InputFileList.push_back(II.getFilename());
518  }
519 
520  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs))
521  addOpenMPRuntime(CmdArgs, getToolChain(), Args);
522 
523  if (isObjCRuntimeLinked(Args) &&
524  !Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
525  // We use arclite library for both ARC and subscripting support.
526  getMachOToolChain().AddLinkARCArgs(Args, CmdArgs);
527 
528  CmdArgs.push_back("-framework");
529  CmdArgs.push_back("Foundation");
530  // Link libobj.
531  CmdArgs.push_back("-lobjc");
532  }
533 
534  if (LinkingOutput) {
535  CmdArgs.push_back("-arch_multiple");
536  CmdArgs.push_back("-final_output");
537  CmdArgs.push_back(LinkingOutput);
538  }
539 
540  if (Args.hasArg(options::OPT_fnested_functions))
541  CmdArgs.push_back("-allow_stack_execute");
542 
543  getMachOToolChain().addProfileRTLibs(Args, CmdArgs);
544 
545  if (unsigned Parallelism =
546  getLTOParallelism(Args, getToolChain().getDriver())) {
547  CmdArgs.push_back("-mllvm");
548  CmdArgs.push_back(
549  Args.MakeArgString(Twine("-threads=") + llvm::to_string(Parallelism)));
550  }
551 
552  if (getToolChain().ShouldLinkCXXStdlib(Args))
553  getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
554  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
555  // link_ssp spec is empty.
556 
557  // Let the tool chain choose which runtime library to link.
558  getMachOToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs);
559 
560  // No need to do anything for pthreads. Claim argument to avoid warning.
561  Args.ClaimAllArgs(options::OPT_pthread);
562  Args.ClaimAllArgs(options::OPT_pthreads);
563  }
564 
565  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
566  // endfile_spec is empty.
567  }
568 
569  Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
570  Args.AddAllArgs(CmdArgs, options::OPT_F);
571 
572  // -iframework should be forwarded as -F.
573  for (const Arg *A : Args.filtered(options::OPT_iframework))
574  CmdArgs.push_back(Args.MakeArgString(std::string("-F") + A->getValue()));
575 
576  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
577  if (Arg *A = Args.getLastArg(options::OPT_fveclib)) {
578  if (A->getValue() == StringRef("Accelerate")) {
579  CmdArgs.push_back("-framework");
580  CmdArgs.push_back("Accelerate");
581  }
582  }
583  }
584 
585  const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
586  std::unique_ptr<Command> Cmd =
587  llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs);
588  Cmd->setInputFileList(std::move(InputFileList));
589  C.addCommand(std::move(Cmd));
590 }
591 
593  const InputInfo &Output,
594  const InputInfoList &Inputs,
595  const ArgList &Args,
596  const char *LinkingOutput) const {
597  ArgStringList CmdArgs;
598 
599  CmdArgs.push_back("-create");
600  assert(Output.isFilename() && "Unexpected lipo output.");
601 
602  CmdArgs.push_back("-output");
603  CmdArgs.push_back(Output.getFilename());
604 
605  for (const auto &II : Inputs) {
606  assert(II.isFilename() && "Unexpected lipo input.");
607  CmdArgs.push_back(II.getFilename());
608  }
609 
610  const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("lipo"));
611  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
612 }
613 
615  const InputInfo &Output,
616  const InputInfoList &Inputs,
617  const ArgList &Args,
618  const char *LinkingOutput) const {
619  ArgStringList CmdArgs;
620 
621  CmdArgs.push_back("-o");
622  CmdArgs.push_back(Output.getFilename());
623 
624  assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
625  const InputInfo &Input = Inputs[0];
626  assert(Input.isFilename() && "Unexpected dsymutil input.");
627  CmdArgs.push_back(Input.getFilename());
628 
629  const char *Exec =
630  Args.MakeArgString(getToolChain().GetProgramPath("dsymutil"));
631  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
632 }
633 
635  const InputInfo &Output,
636  const InputInfoList &Inputs,
637  const ArgList &Args,
638  const char *LinkingOutput) const {
639  ArgStringList CmdArgs;
640  CmdArgs.push_back("--verify");
641  CmdArgs.push_back("--debug-info");
642  CmdArgs.push_back("--eh-frame");
643  CmdArgs.push_back("--quiet");
644 
645  assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
646  const InputInfo &Input = Inputs[0];
647  assert(Input.isFilename() && "Unexpected verify input");
648 
649  // Grabbing the output of the earlier dsymutil run.
650  CmdArgs.push_back(Input.getFilename());
651 
652  const char *Exec =
653  Args.MakeArgString(getToolChain().GetProgramPath("dwarfdump"));
654  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
655 }
656 
657 MachO::MachO(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
658  : ToolChain(D, Triple, Args) {
659  // We expect 'as', 'ld', etc. to be adjacent to our install dir.
660  getProgramPaths().push_back(getDriver().getInstalledDir());
661  if (getDriver().getInstalledDir() != getDriver().Dir)
662  getProgramPaths().push_back(getDriver().Dir);
663 }
664 
665 /// Darwin - Darwin tool chain for i386 and x86_64.
666 Darwin::Darwin(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
667  : MachO(D, Triple, Args), TargetInitialized(false),
668  CudaInstallation(D, Triple, Args) {}
669 
672 
673  // Darwin always preprocesses assembly files (unless -x is used explicitly).
674  if (Ty == types::TY_PP_Asm)
675  return types::TY_Asm;
676 
677  return Ty;
678 }
679 
680 bool MachO::HasNativeLLVMSupport() const { return true; }
681 
683  // Default to use libc++ on OS X 10.9+ and iOS 7+.
684  if ((isTargetMacOS() && !isMacosxVersionLT(10, 9)) ||
685  (isTargetIOSBased() && !isIPhoneOSVersionLT(7, 0)) ||
687  return ToolChain::CST_Libcxx;
688 
690 }
691 
692 /// Darwin provides an ARC runtime starting in MacOS X 10.7 and iOS 5.0.
693 ObjCRuntime Darwin::getDefaultObjCRuntime(bool isNonFragile) const {
694  if (isTargetWatchOSBased())
696  if (isTargetIOSBased())
698  if (isNonFragile)
701 }
702 
703 /// Darwin provides a blocks runtime starting in MacOS X 10.6 and iOS 3.2.
705  if (isTargetWatchOSBased())
706  return true;
707  else if (isTargetIOSBased())
708  return !isIPhoneOSVersionLT(3, 2);
709  else {
710  assert(isTargetMacOS() && "unexpected darwin target");
711  return !isMacosxVersionLT(10, 6);
712  }
713 }
714 
715 void Darwin::AddCudaIncludeArgs(const ArgList &DriverArgs,
716  ArgStringList &CC1Args) const {
717  CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args);
718 }
719 
720 // This is just a MachO name translation routine and there's no
721 // way to join this into ARMTargetParser without breaking all
722 // other assumptions. Maybe MachO should consider standardising
723 // their nomenclature.
724 static const char *ArmMachOArchName(StringRef Arch) {
725  return llvm::StringSwitch<const char *>(Arch)
726  .Case("armv6k", "armv6")
727  .Case("armv6m", "armv6m")
728  .Case("armv5tej", "armv5")
729  .Case("xscale", "xscale")
730  .Case("armv4t", "armv4t")
731  .Case("armv7", "armv7")
732  .Cases("armv7a", "armv7-a", "armv7")
733  .Cases("armv7r", "armv7-r", "armv7")
734  .Cases("armv7em", "armv7e-m", "armv7em")
735  .Cases("armv7k", "armv7-k", "armv7k")
736  .Cases("armv7m", "armv7-m", "armv7m")
737  .Cases("armv7s", "armv7-s", "armv7s")
738  .Default(nullptr);
739 }
740 
741 static const char *ArmMachOArchNameCPU(StringRef CPU) {
742  llvm::ARM::ArchKind ArchKind = llvm::ARM::parseCPUArch(CPU);
743  if (ArchKind == llvm::ARM::ArchKind::INVALID)
744  return nullptr;
745  StringRef Arch = llvm::ARM::getArchName(ArchKind);
746 
747  // FIXME: Make sure this MachO triple mangling is really necessary.
748  // ARMv5* normalises to ARMv5.
749  if (Arch.startswith("armv5"))
750  Arch = Arch.substr(0, 5);
751  // ARMv6*, except ARMv6M, normalises to ARMv6.
752  else if (Arch.startswith("armv6") && !Arch.endswith("6m"))
753  Arch = Arch.substr(0, 5);
754  // ARMv7A normalises to ARMv7.
755  else if (Arch.endswith("v7a"))
756  Arch = Arch.substr(0, 5);
757  return Arch.data();
758 }
759 
760 StringRef MachO::getMachOArchName(const ArgList &Args) const {
761  switch (getTriple().getArch()) {
762  default:
764 
765  case llvm::Triple::aarch64:
766  return "arm64";
767 
768  case llvm::Triple::thumb:
769  case llvm::Triple::arm:
770  if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_march_EQ))
771  if (const char *Arch = ArmMachOArchName(A->getValue()))
772  return Arch;
773 
774  if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
775  if (const char *Arch = ArmMachOArchNameCPU(A->getValue()))
776  return Arch;
777 
778  return "arm";
779  }
780 }
781 
783 
785 
786 std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args,
787  types::ID InputType) const {
788  llvm::Triple Triple(ComputeLLVMTriple(Args, InputType));
789 
790  // If the target isn't initialized (e.g., an unknown Darwin platform, return
791  // the default triple).
792  if (!isTargetInitialized())
793  return Triple.getTriple();
794 
795  SmallString<16> Str;
796  if (isTargetWatchOSBased())
797  Str += "watchos";
798  else if (isTargetTvOSBased())
799  Str += "tvos";
800  else if (isTargetIOSBased())
801  Str += "ios";
802  else
803  Str += "macosx";
804  Str += getTargetVersion().getAsString();
805  Triple.setOSName(Str);
806 
807  return Triple.getTriple();
808 }
809 
811  switch (AC) {
813  if (!Lipo)
814  Lipo.reset(new tools::darwin::Lipo(*this));
815  return Lipo.get();
817  if (!Dsymutil)
818  Dsymutil.reset(new tools::darwin::Dsymutil(*this));
819  return Dsymutil.get();
821  if (!VerifyDebug)
822  VerifyDebug.reset(new tools::darwin::VerifyDebug(*this));
823  return VerifyDebug.get();
824  default:
825  return ToolChain::getTool(AC);
826  }
827 }
828 
829 Tool *MachO::buildLinker() const { return new tools::darwin::Linker(*this); }
830 
832  return new tools::darwin::Assembler(*this);
833 }
834 
835 DarwinClang::DarwinClang(const Driver &D, const llvm::Triple &Triple,
836  const ArgList &Args)
837  : Darwin(D, Triple, Args) {}
838 
839 void DarwinClang::addClangWarningOptions(ArgStringList &CC1Args) const {
840  // For modern targets, promote certain warnings to errors.
841  if (isTargetWatchOSBased() || getTriple().isArch64Bit()) {
842  // Always enable -Wdeprecated-objc-isa-usage and promote it
843  // to an error.
844  CC1Args.push_back("-Wdeprecated-objc-isa-usage");
845  CC1Args.push_back("-Werror=deprecated-objc-isa-usage");
846 
847  // For iOS and watchOS, also error about implicit function declarations,
848  // as that can impact calling conventions.
849  if (!isTargetMacOS())
850  CC1Args.push_back("-Werror=implicit-function-declaration");
851  }
852 }
853 
854 void DarwinClang::AddLinkARCArgs(const ArgList &Args,
855  ArgStringList &CmdArgs) const {
856  // Avoid linking compatibility stubs on i386 mac.
857  if (isTargetMacOS() && getArch() == llvm::Triple::x86)
858  return;
859 
860  ObjCRuntime runtime = getDefaultObjCRuntime(/*nonfragile*/ true);
861 
862  if ((runtime.hasNativeARC() || !isObjCAutoRefCount(Args)) &&
863  runtime.hasSubscripting())
864  return;
865 
866  CmdArgs.push_back("-force_load");
867  SmallString<128> P(getDriver().ClangExecutable);
868  llvm::sys::path::remove_filename(P); // 'clang'
869  llvm::sys::path::remove_filename(P); // 'bin'
870  llvm::sys::path::append(P, "lib", "arc", "libarclite_");
871  // Mash in the platform.
873  P += "watchsimulator";
874  else if (isTargetWatchOS())
875  P += "watchos";
876  else if (isTargetTvOSSimulator())
877  P += "appletvsimulator";
878  else if (isTargetTvOS())
879  P += "appletvos";
880  else if (isTargetIOSSimulator())
881  P += "iphonesimulator";
882  else if (isTargetIPhoneOS())
883  P += "iphoneos";
884  else
885  P += "macosx";
886  P += ".a";
887 
888  CmdArgs.push_back(Args.MakeArgString(P));
889 }
890 
892  // Default to use DWARF 2 on OS X 10.10 / iOS 8 and lower.
893  if ((isTargetMacOS() && isMacosxVersionLT(10, 11)) ||
895  return 2;
896  return 4;
897 }
898 
899 void MachO::AddLinkRuntimeLib(const ArgList &Args, ArgStringList &CmdArgs,
900  StringRef DarwinLibName,
901  RuntimeLinkOptions Opts) const {
902  SmallString<128> Dir(getDriver().ResourceDir);
903  llvm::sys::path::append(
904  Dir, "lib", (Opts & RLO_IsEmbedded) ? "macho_embedded" : "darwin");
905 
906  SmallString<128> P(Dir);
907  llvm::sys::path::append(P, DarwinLibName);
908 
909  // For now, allow missing resource libraries to support developers who may
910  // not have compiler-rt checked out or integrated into their build (unless
911  // we explicitly force linking with this library).
912  if ((Opts & RLO_AlwaysLink) || getVFS().exists(P)) {
913  const char *LibArg = Args.MakeArgString(P);
914  if (Opts & RLO_FirstLink)
915  CmdArgs.insert(CmdArgs.begin(), LibArg);
916  else
917  CmdArgs.push_back(LibArg);
918  }
919 
920  // Adding the rpaths might negatively interact when other rpaths are involved,
921  // so we should make sure we add the rpaths last, after all user-specified
922  // rpaths. This is currently true from this place, but we need to be
923  // careful if this function is ever called before user's rpaths are emitted.
924  if (Opts & RLO_AddRPath) {
925  assert(DarwinLibName.endswith(".dylib") && "must be a dynamic library");
926 
927  // Add @executable_path to rpath to support having the dylib copied with
928  // the executable.
929  CmdArgs.push_back("-rpath");
930  CmdArgs.push_back("@executable_path");
931 
932  // Add the path to the resource dir to rpath to support using the dylib
933  // from the default location without copying.
934  CmdArgs.push_back("-rpath");
935  CmdArgs.push_back(Args.MakeArgString(Dir));
936  }
937 }
938 
939 StringRef Darwin::getPlatformFamily() const {
940  switch (TargetPlatform) {
941  case DarwinPlatformKind::MacOS:
942  return "MacOSX";
943  case DarwinPlatformKind::IPhoneOS:
944  case DarwinPlatformKind::IPhoneOSSimulator:
945  return "iPhone";
946  case DarwinPlatformKind::TvOS:
947  case DarwinPlatformKind::TvOSSimulator:
948  return "AppleTV";
949  case DarwinPlatformKind::WatchOS:
950  case DarwinPlatformKind::WatchOSSimulator:
951  return "Watch";
952  }
953  llvm_unreachable("Unsupported platform");
954 }
955 
956 StringRef Darwin::getSDKName(StringRef isysroot) {
957  // Assume SDK has path: SOME_PATH/SDKs/PlatformXX.YY.sdk
958  llvm::sys::path::const_iterator SDKDir;
959  auto BeginSDK = llvm::sys::path::begin(isysroot);
960  auto EndSDK = llvm::sys::path::end(isysroot);
961  for (auto IT = BeginSDK; IT != EndSDK; ++IT) {
962  StringRef SDK = *IT;
963  if (SDK.endswith(".sdk"))
964  return SDK.slice(0, SDK.size() - 4);
965  }
966  return "";
967 }
968 
970  switch(TargetPlatform) {
971  case DarwinPlatformKind::MacOS:
972  return "osx";
973  case DarwinPlatformKind::IPhoneOS:
974  return "ios";
975  case DarwinPlatformKind::IPhoneOSSimulator:
976  return "iossim";
977  case DarwinPlatformKind::TvOS:
978  return "tvos";
979  case DarwinPlatformKind::TvOSSimulator:
980  return "tvossim";
981  case DarwinPlatformKind::WatchOS:
982  return "watchos";
983  case DarwinPlatformKind::WatchOSSimulator:
984  return "watchossim";
985  }
986  llvm_unreachable("Unsupported platform");
987 }
988 
989 /// Check if the link command contains a symbol export directive.
990 static bool hasExportSymbolDirective(const ArgList &Args) {
991  for (Arg *A : Args) {
992  if (!A->getOption().matches(options::OPT_Wl_COMMA) &&
993  !A->getOption().matches(options::OPT_Xlinker))
994  continue;
995  if (A->containsValue("-exported_symbols_list") ||
996  A->containsValue("-exported_symbol"))
997  return true;
998  }
999  return false;
1000 }
1001 
1002 /// Add an export directive for \p Symbol to the link command.
1003 static void addExportedSymbol(ArgStringList &CmdArgs, const char *Symbol) {
1004  CmdArgs.push_back("-exported_symbol");
1005  CmdArgs.push_back(Symbol);
1006 }
1007 
1008 void Darwin::addProfileRTLibs(const ArgList &Args,
1009  ArgStringList &CmdArgs) const {
1010  if (!needsProfileRT(Args)) return;
1011 
1013  Args, CmdArgs,
1014  (Twine("libclang_rt.profile_") + getOSLibraryNameSuffix() + ".a").str(),
1016 
1017  // If we have a symbol export directive and we're linking in the profile
1018  // runtime, automatically export symbols necessary to implement some of the
1019  // runtime's functionality.
1020  if (hasExportSymbolDirective(Args)) {
1021  addExportedSymbol(CmdArgs, "_VPMergeHook");
1022  addExportedSymbol(CmdArgs, "___llvm_profile_filename");
1023  addExportedSymbol(CmdArgs, "___llvm_profile_raw_version");
1024  addExportedSymbol(CmdArgs, "_lprofCurFilename");
1025  }
1026 }
1027 
1028 void DarwinClang::AddLinkSanitizerLibArgs(const ArgList &Args,
1029  ArgStringList &CmdArgs,
1030  StringRef Sanitizer,
1031  bool Shared) const {
1032  auto RLO = RuntimeLinkOptions(RLO_AlwaysLink | (Shared ? RLO_AddRPath : 0U));
1033  AddLinkRuntimeLib(Args, CmdArgs,
1034  (Twine("libclang_rt.") + Sanitizer + "_" +
1036  (Shared ? "_dynamic.dylib" : ".a"))
1037  .str(),
1038  RLO);
1039 }
1040 
1042  const ArgList &Args) const {
1043  if (Arg* A = Args.getLastArg(options::OPT_rtlib_EQ)) {
1044  StringRef Value = A->getValue();
1045  if (Value != "compiler-rt")
1046  getDriver().Diag(clang::diag::err_drv_unsupported_rtlib_for_platform)
1047  << Value << "darwin";
1048  }
1049 
1051 }
1052 
1053 void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
1054  ArgStringList &CmdArgs) const {
1055  // Call once to ensure diagnostic is printed if wrong value was specified
1056  GetRuntimeLibType(Args);
1057 
1058  // Darwin doesn't support real static executables, don't link any runtime
1059  // libraries with -static.
1060  if (Args.hasArg(options::OPT_static) ||
1061  Args.hasArg(options::OPT_fapple_kext) ||
1062  Args.hasArg(options::OPT_mkernel))
1063  return;
1064 
1065  // Reject -static-libgcc for now, we can deal with this when and if someone
1066  // cares. This is useful in situations where someone wants to statically link
1067  // something like libstdc++, and needs its runtime support routines.
1068  if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
1069  getDriver().Diag(diag::err_drv_unsupported_opt) << A->getAsString(Args);
1070  return;
1071  }
1072 
1073  const SanitizerArgs &Sanitize = getSanitizerArgs();
1074  if (Sanitize.needsAsanRt())
1075  AddLinkSanitizerLibArgs(Args, CmdArgs, "asan");
1076  if (Sanitize.needsLsanRt())
1077  AddLinkSanitizerLibArgs(Args, CmdArgs, "lsan");
1078  if (Sanitize.needsUbsanRt())
1079  AddLinkSanitizerLibArgs(Args, CmdArgs,
1080  Sanitize.requiresMinimalRuntime() ? "ubsan_minimal"
1081  : "ubsan",
1082  Sanitize.needsSharedRt());
1083  if (Sanitize.needsTsanRt())
1084  AddLinkSanitizerLibArgs(Args, CmdArgs, "tsan");
1085  if (Sanitize.needsFuzzer() && !Args.hasArg(options::OPT_dynamiclib)) {
1086  AddLinkSanitizerLibArgs(Args, CmdArgs, "fuzzer", /*shared=*/false);
1087 
1088  // Libfuzzer is written in C++ and requires libcxx.
1089  AddCXXStdlibLibArgs(Args, CmdArgs);
1090  }
1091  if (Sanitize.needsStatsRt()) {
1092  StringRef OS = isTargetMacOS() ? "osx" : "iossim";
1093  AddLinkRuntimeLib(Args, CmdArgs,
1094  (Twine("libclang_rt.stats_client_") + OS + ".a").str(),
1095  RLO_AlwaysLink);
1096  AddLinkSanitizerLibArgs(Args, CmdArgs, "stats");
1097  }
1098  if (Sanitize.needsEsanRt())
1099  AddLinkSanitizerLibArgs(Args, CmdArgs, "esan");
1100 
1101  // Otherwise link libSystem, then the dynamic runtime library, and finally any
1102  // target specific static runtime library.
1103  CmdArgs.push_back("-lSystem");
1104 
1105  // Select the dynamic runtime library and the target specific static library.
1106  if (isTargetWatchOSBased()) {
1107  // We currently always need a static runtime library for watchOS.
1108  AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.watchos.a");
1109  } else if (isTargetTvOSBased()) {
1110  // We currently always need a static runtime library for tvOS.
1111  AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.tvos.a");
1112  } else if (isTargetIOSBased()) {
1113  // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
1114  // it never went into the SDK.
1115  // Linking against libgcc_s.1 isn't needed for iOS 5.0+
1116  if (isIPhoneOSVersionLT(5, 0) && !isTargetIOSSimulator() &&
1117  getTriple().getArch() != llvm::Triple::aarch64)
1118  CmdArgs.push_back("-lgcc_s.1");
1119 
1120  // We currently always need a static runtime library for iOS.
1121  AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.ios.a");
1122  } else {
1123  assert(isTargetMacOS() && "unexpected non MacOS platform");
1124  // The dynamic runtime library was merged with libSystem for 10.6 and
1125  // beyond; only 10.4 and 10.5 need an additional runtime library.
1126  if (isMacosxVersionLT(10, 5))
1127  CmdArgs.push_back("-lgcc_s.10.4");
1128  else if (isMacosxVersionLT(10, 6))
1129  CmdArgs.push_back("-lgcc_s.10.5");
1130 
1131  // Originally for OS X, we thought we would only need a static runtime
1132  // library when targeting 10.4, to provide versions of the static functions
1133  // which were omitted from 10.4.dylib. This led to the creation of the 10.4
1134  // builtins library.
1135  //
1136  // Unfortunately, that turned out to not be true, because Darwin system
1137  // headers can still use eprintf on i386, and it is not exported from
1138  // libSystem. Therefore, we still must provide a runtime library just for
1139  // the tiny tiny handful of projects that *might* use that symbol.
1140  //
1141  // Then over time, we figured out it was useful to add more things to the
1142  // runtime so we created libclang_rt.osx.a to provide new functions when
1143  // deploying to old OS builds, and for a long time we had both eprintf and
1144  // osx builtin libraries. Which just seems excessive. So with PR 28855, we
1145  // are removing the eprintf library and expecting eprintf to be provided by
1146  // the OS X builtins library.
1147  if (isMacosxVersionLT(10, 5))
1148  AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.10.4.a");
1149  else
1150  AddLinkRuntimeLib(Args, CmdArgs, "libclang_rt.osx.a");
1151  }
1152 }
1153 
1154 /// Returns the most appropriate macOS target version for the current process.
1155 ///
1156 /// If the macOS SDK version is the same or earlier than the system version,
1157 /// then the SDK version is returned. Otherwise the system version is returned.
1158 static std::string getSystemOrSDKMacOSVersion(StringRef MacOSSDKVersion) {
1159  unsigned Major, Minor, Micro;
1160  llvm::Triple SystemTriple(llvm::sys::getProcessTriple());
1161  if (!SystemTriple.isMacOSX())
1162  return MacOSSDKVersion;
1163  SystemTriple.getMacOSXVersion(Major, Minor, Micro);
1164  VersionTuple SystemVersion(Major, Minor, Micro);
1165  bool HadExtra;
1166  if (!Driver::GetReleaseVersion(MacOSSDKVersion, Major, Minor, Micro,
1167  HadExtra))
1168  return MacOSSDKVersion;
1169  VersionTuple SDKVersion(Major, Minor, Micro);
1170  if (SDKVersion > SystemVersion)
1171  return SystemVersion.getAsString();
1172  return MacOSSDKVersion;
1173 }
1174 
1175 void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
1176  const OptTable &Opts = getDriver().getOpts();
1177 
1178  // Support allowing the SDKROOT environment variable used by xcrun and other
1179  // Xcode tools to define the default sysroot, by making it the default for
1180  // isysroot.
1181  if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
1182  // Warn if the path does not exist.
1183  if (!getVFS().exists(A->getValue()))
1184  getDriver().Diag(clang::diag::warn_missing_sysroot) << A->getValue();
1185  } else {
1186  if (char *env = ::getenv("SDKROOT")) {
1187  // We only use this value as the default if it is an absolute path,
1188  // exists, and it is not the root path.
1189  if (llvm::sys::path::is_absolute(env) && getVFS().exists(env) &&
1190  StringRef(env) != "/") {
1191  Args.append(Args.MakeSeparateArg(
1192  nullptr, Opts.getOption(options::OPT_isysroot), env));
1193  }
1194  }
1195  }
1196 
1197  Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
1198  Arg *iOSVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ,
1199  options::OPT_mios_simulator_version_min_EQ);
1200  Arg *TvOSVersion =
1201  Args.getLastArg(options::OPT_mtvos_version_min_EQ,
1202  options::OPT_mtvos_simulator_version_min_EQ);
1203  Arg *WatchOSVersion =
1204  Args.getLastArg(options::OPT_mwatchos_version_min_EQ,
1205  options::OPT_mwatchos_simulator_version_min_EQ);
1206 
1207  unsigned Major, Minor, Micro;
1208  bool HadExtra;
1209 
1210  // The iOS deployment target that is explicitly specified via a command line
1211  // option or an environment variable.
1212  std::string ExplicitIOSDeploymentTargetStr;
1213 
1214  if (iOSVersion)
1215  ExplicitIOSDeploymentTargetStr = iOSVersion->getAsString(Args);
1216 
1217  // Add a macro to differentiate between m(iphone|tv|watch)os-version-min=X.Y and
1218  // -m(iphone|tv|watch)simulator-version-min=X.Y.
1219  if (Args.hasArg(options::OPT_mios_simulator_version_min_EQ) ||
1220  Args.hasArg(options::OPT_mtvos_simulator_version_min_EQ) ||
1221  Args.hasArg(options::OPT_mwatchos_simulator_version_min_EQ))
1222  Args.append(Args.MakeSeparateArg(nullptr, Opts.getOption(options::OPT_D),
1223  " __APPLE_EMBEDDED_SIMULATOR__=1"));
1224 
1225  if (OSXVersion && (iOSVersion || TvOSVersion || WatchOSVersion)) {
1226  getDriver().Diag(diag::err_drv_argument_not_allowed_with)
1227  << OSXVersion->getAsString(Args)
1228  << (iOSVersion ? iOSVersion :
1229  TvOSVersion ? TvOSVersion : WatchOSVersion)->getAsString(Args);
1230  iOSVersion = TvOSVersion = WatchOSVersion = nullptr;
1231  } else if (iOSVersion && (TvOSVersion || WatchOSVersion)) {
1232  getDriver().Diag(diag::err_drv_argument_not_allowed_with)
1233  << iOSVersion->getAsString(Args)
1234  << (TvOSVersion ? TvOSVersion : WatchOSVersion)->getAsString(Args);
1235  TvOSVersion = WatchOSVersion = nullptr;
1236  } else if (TvOSVersion && WatchOSVersion) {
1237  getDriver().Diag(diag::err_drv_argument_not_allowed_with)
1238  << TvOSVersion->getAsString(Args)
1239  << WatchOSVersion->getAsString(Args);
1240  WatchOSVersion = nullptr;
1241  } else if (!OSXVersion && !iOSVersion && !TvOSVersion && !WatchOSVersion) {
1242  // If no deployment target was specified on the command line, check for
1243  // environment defines.
1244  std::string OSXTarget;
1245  std::string iOSTarget;
1246  std::string TvOSTarget;
1247  std::string WatchOSTarget;
1248 
1249  if (char *env = ::getenv("MACOSX_DEPLOYMENT_TARGET"))
1250  OSXTarget = env;
1251  if (char *env = ::getenv("IPHONEOS_DEPLOYMENT_TARGET"))
1252  iOSTarget = env;
1253  if (char *env = ::getenv("TVOS_DEPLOYMENT_TARGET"))
1254  TvOSTarget = env;
1255  if (char *env = ::getenv("WATCHOS_DEPLOYMENT_TARGET"))
1256  WatchOSTarget = env;
1257 
1258  if (!iOSTarget.empty())
1259  ExplicitIOSDeploymentTargetStr =
1260  std::string("IPHONEOS_DEPLOYMENT_TARGET=") + iOSTarget;
1261 
1262  // If there is no command-line argument to specify the Target version and
1263  // no environment variable defined, see if we can set the default based
1264  // on -isysroot.
1265  if (OSXTarget.empty() && iOSTarget.empty() && WatchOSTarget.empty() &&
1266  TvOSTarget.empty() && Args.hasArg(options::OPT_isysroot)) {
1267  if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
1268  StringRef isysroot = A->getValue();
1269  StringRef SDK = getSDKName(isysroot);
1270  if (SDK.size() > 0) {
1271  // Slice the version number out.
1272  // Version number is between the first and the last number.
1273  size_t StartVer = SDK.find_first_of("0123456789");
1274  size_t EndVer = SDK.find_last_of("0123456789");
1275  if (StartVer != StringRef::npos && EndVer > StartVer) {
1276  StringRef Version = SDK.slice(StartVer, EndVer + 1);
1277  if (SDK.startswith("iPhoneOS") ||
1278  SDK.startswith("iPhoneSimulator"))
1279  iOSTarget = Version;
1280  else if (SDK.startswith("MacOSX"))
1281  OSXTarget = getSystemOrSDKMacOSVersion(Version);
1282  else if (SDK.startswith("WatchOS") ||
1283  SDK.startswith("WatchSimulator"))
1284  WatchOSTarget = Version;
1285  else if (SDK.startswith("AppleTVOS") ||
1286  SDK.startswith("AppleTVSimulator"))
1287  TvOSTarget = Version;
1288  }
1289  }
1290  }
1291  }
1292 
1293  // If no OS targets have been specified, try to guess platform from -target
1294  // or arch name and compute the version from the triple.
1295  if (OSXTarget.empty() && iOSTarget.empty() && TvOSTarget.empty() &&
1296  WatchOSTarget.empty()) {
1297  llvm::Triple::OSType OSTy = llvm::Triple::UnknownOS;
1298 
1299  // Set the OSTy based on -target if -arch isn't present.
1300  if (Args.hasArg(options::OPT_target) && !Args.hasArg(options::OPT_arch)) {
1301  OSTy = getTriple().getOS();
1302  } else {
1303  StringRef MachOArchName = getMachOArchName(Args);
1304  if (MachOArchName == "armv7" || MachOArchName == "armv7s" ||
1305  MachOArchName == "arm64")
1306  OSTy = llvm::Triple::IOS;
1307  else if (MachOArchName == "armv7k")
1308  OSTy = llvm::Triple::WatchOS;
1309  else if (MachOArchName != "armv6m" && MachOArchName != "armv7m" &&
1310  MachOArchName != "armv7em")
1311  OSTy = llvm::Triple::MacOSX;
1312  }
1313 
1314 
1315  if (OSTy != llvm::Triple::UnknownOS) {
1316  unsigned Major, Minor, Micro;
1317  std::string *OSTarget;
1318 
1319  switch (OSTy) {
1320  case llvm::Triple::Darwin:
1321  case llvm::Triple::MacOSX:
1322  if (!getTriple().getMacOSXVersion(Major, Minor, Micro))
1323  getDriver().Diag(diag::err_drv_invalid_darwin_version)
1324  << getTriple().getOSName();
1325  OSTarget = &OSXTarget;
1326  break;
1327  case llvm::Triple::IOS:
1328  getTriple().getiOSVersion(Major, Minor, Micro);
1329  OSTarget = &iOSTarget;
1330  break;
1331  case llvm::Triple::TvOS:
1332  getTriple().getOSVersion(Major, Minor, Micro);
1333  OSTarget = &TvOSTarget;
1334  break;
1335  case llvm::Triple::WatchOS:
1336  getTriple().getWatchOSVersion(Major, Minor, Micro);
1337  OSTarget = &WatchOSTarget;
1338  break;
1339  default:
1340  llvm_unreachable("Unexpected OS type");
1341  break;
1342  }
1343 
1344  llvm::raw_string_ostream(*OSTarget) << Major << '.' << Minor << '.'
1345  << Micro;
1346  }
1347  }
1348 
1349  // Do not allow conflicts with the watchOS target.
1350  if (!WatchOSTarget.empty() && (!iOSTarget.empty() || !TvOSTarget.empty())) {
1351  getDriver().Diag(diag::err_drv_conflicting_deployment_targets)
1352  << "WATCHOS_DEPLOYMENT_TARGET"
1353  << (!iOSTarget.empty() ? "IPHONEOS_DEPLOYMENT_TARGET" :
1354  "TVOS_DEPLOYMENT_TARGET");
1355  }
1356 
1357  // Do not allow conflicts with the tvOS target.
1358  if (!TvOSTarget.empty() && !iOSTarget.empty()) {
1359  getDriver().Diag(diag::err_drv_conflicting_deployment_targets)
1360  << "TVOS_DEPLOYMENT_TARGET"
1361  << "IPHONEOS_DEPLOYMENT_TARGET";
1362  }
1363 
1364  // Allow conflicts among OSX and iOS for historical reasons, but choose the
1365  // default platform.
1366  if (!OSXTarget.empty() && (!iOSTarget.empty() ||
1367  !WatchOSTarget.empty() ||
1368  !TvOSTarget.empty())) {
1369  if (getTriple().getArch() == llvm::Triple::arm ||
1370  getTriple().getArch() == llvm::Triple::aarch64 ||
1371  getTriple().getArch() == llvm::Triple::thumb)
1372  OSXTarget = "";
1373  else
1374  iOSTarget = WatchOSTarget = TvOSTarget = "";
1375  }
1376 
1377  if (!OSXTarget.empty()) {
1378  const Option O = Opts.getOption(options::OPT_mmacosx_version_min_EQ);
1379  OSXVersion = Args.MakeJoinedArg(nullptr, O, OSXTarget);
1380  Args.append(OSXVersion);
1381  } else if (!iOSTarget.empty()) {
1382  const Option O = Opts.getOption(options::OPT_miphoneos_version_min_EQ);
1383  iOSVersion = Args.MakeJoinedArg(nullptr, O, iOSTarget);
1384  Args.append(iOSVersion);
1385  } else if (!TvOSTarget.empty()) {
1386  const Option O = Opts.getOption(options::OPT_mtvos_version_min_EQ);
1387  TvOSVersion = Args.MakeJoinedArg(nullptr, O, TvOSTarget);
1388  Args.append(TvOSVersion);
1389  } else if (!WatchOSTarget.empty()) {
1390  const Option O = Opts.getOption(options::OPT_mwatchos_version_min_EQ);
1391  WatchOSVersion = Args.MakeJoinedArg(nullptr, O, WatchOSTarget);
1392  Args.append(WatchOSVersion);
1393  }
1394  }
1395 
1396  DarwinPlatformKind Platform;
1397  if (OSXVersion)
1398  Platform = MacOS;
1399  else if (iOSVersion)
1400  Platform = IPhoneOS;
1401  else if (TvOSVersion)
1402  Platform = TvOS;
1403  else if (WatchOSVersion)
1404  Platform = WatchOS;
1405  else
1406  llvm_unreachable("Unable to infer Darwin variant");
1407 
1408  // Set the tool chain target information.
1409  if (Platform == MacOS) {
1410  assert((!iOSVersion && !TvOSVersion && !WatchOSVersion) &&
1411  "Unknown target platform!");
1412  if (!Driver::GetReleaseVersion(OSXVersion->getValue(), Major, Minor, Micro,
1413  HadExtra) ||
1414  HadExtra || Major != 10 || Minor >= 100 || Micro >= 100)
1415  getDriver().Diag(diag::err_drv_invalid_version_number)
1416  << OSXVersion->getAsString(Args);
1417  } else if (Platform == IPhoneOS) {
1418  assert(iOSVersion && "Unknown target platform!");
1419  if (!Driver::GetReleaseVersion(iOSVersion->getValue(), Major, Minor, Micro,
1420  HadExtra) ||
1421  HadExtra || Major >= 100 || Minor >= 100 || Micro >= 100)
1422  getDriver().Diag(diag::err_drv_invalid_version_number)
1423  << iOSVersion->getAsString(Args);
1424  // For 32-bit targets, the deployment target for iOS has to be earlier than
1425  // iOS 11.
1426  if (getTriple().isArch32Bit() && Major >= 11) {
1427  // If the deployment target is explicitly specified, print a diagnostic.
1428  if (!ExplicitIOSDeploymentTargetStr.empty()) {
1429  getDriver().Diag(diag::warn_invalid_ios_deployment_target)
1430  << ExplicitIOSDeploymentTargetStr;
1431  // Otherwise, set it to 10.99.99.
1432  } else {
1433  Major = 10;
1434  Minor = 99;
1435  Micro = 99;
1436  }
1437  }
1438  } else if (Platform == TvOS) {
1439  if (!Driver::GetReleaseVersion(TvOSVersion->getValue(), Major, Minor,
1440  Micro, HadExtra) || HadExtra ||
1441  Major >= 100 || Minor >= 100 || Micro >= 100)
1442  getDriver().Diag(diag::err_drv_invalid_version_number)
1443  << TvOSVersion->getAsString(Args);
1444  } else if (Platform == WatchOS) {
1445  if (!Driver::GetReleaseVersion(WatchOSVersion->getValue(), Major, Minor,
1446  Micro, HadExtra) || HadExtra ||
1447  Major >= 10 || Minor >= 100 || Micro >= 100)
1448  getDriver().Diag(diag::err_drv_invalid_version_number)
1449  << WatchOSVersion->getAsString(Args);
1450  } else
1451  llvm_unreachable("unknown kind of Darwin platform");
1452 
1453  // Recognize iOS targets with an x86 architecture as the iOS simulator.
1454  if (iOSVersion && (getTriple().getArch() == llvm::Triple::x86 ||
1455  getTriple().getArch() == llvm::Triple::x86_64))
1456  Platform = IPhoneOSSimulator;
1457  if (TvOSVersion && (getTriple().getArch() == llvm::Triple::x86 ||
1458  getTriple().getArch() == llvm::Triple::x86_64))
1459  Platform = TvOSSimulator;
1460  if (WatchOSVersion && (getTriple().getArch() == llvm::Triple::x86 ||
1461  getTriple().getArch() == llvm::Triple::x86_64))
1462  Platform = WatchOSSimulator;
1463 
1464  setTarget(Platform, Major, Minor, Micro);
1465 
1466  if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
1467  StringRef SDK = getSDKName(A->getValue());
1468  if (SDK.size() > 0) {
1469  size_t StartVer = SDK.find_first_of("0123456789");
1470  StringRef SDKName = SDK.slice(0, StartVer);
1471  if (!SDKName.startswith(getPlatformFamily()))
1472  getDriver().Diag(diag::warn_incompatible_sysroot)
1473  << SDKName << getPlatformFamily();
1474  }
1475  }
1476 }
1477 
1478 void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
1479  ArgStringList &CmdArgs) const {
1481 
1482  switch (Type) {
1483  case ToolChain::CST_Libcxx:
1484  CmdArgs.push_back("-lc++");
1485  break;
1486 
1488  // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
1489  // it was previously found in the gcc lib dir. However, for all the Darwin
1490  // platforms we care about it was -lstdc++.6, so we search for that
1491  // explicitly if we can't see an obvious -lstdc++ candidate.
1492 
1493  // Check in the sysroot first.
1494  if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
1495  SmallString<128> P(A->getValue());
1496  llvm::sys::path::append(P, "usr", "lib", "libstdc++.dylib");
1497 
1498  if (!getVFS().exists(P)) {
1499  llvm::sys::path::remove_filename(P);
1500  llvm::sys::path::append(P, "libstdc++.6.dylib");
1501  if (getVFS().exists(P)) {
1502  CmdArgs.push_back(Args.MakeArgString(P));
1503  return;
1504  }
1505  }
1506  }
1507 
1508  // Otherwise, look in the root.
1509  // FIXME: This should be removed someday when we don't have to care about
1510  // 10.6 and earlier, where /usr/lib/libstdc++.dylib does not exist.
1511  if (!getVFS().exists("/usr/lib/libstdc++.dylib") &&
1512  getVFS().exists("/usr/lib/libstdc++.6.dylib")) {
1513  CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
1514  return;
1515  }
1516 
1517  // Otherwise, let the linker search.
1518  CmdArgs.push_back("-lstdc++");
1519  break;
1520  }
1521 }
1522 
1523 void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
1524  ArgStringList &CmdArgs) const {
1525  // For Darwin platforms, use the compiler-rt-based support library
1526  // instead of the gcc-provided one (which is also incidentally
1527  // only present in the gcc lib dir, which makes it hard to find).
1528 
1529  SmallString<128> P(getDriver().ResourceDir);
1530  llvm::sys::path::append(P, "lib", "darwin");
1531 
1532  // Use the newer cc_kext for iOS ARM after 6.0.
1533  if (isTargetWatchOS()) {
1534  llvm::sys::path::append(P, "libclang_rt.cc_kext_watchos.a");
1535  } else if (isTargetTvOS()) {
1536  llvm::sys::path::append(P, "libclang_rt.cc_kext_tvos.a");
1537  } else if (isTargetIPhoneOS()) {
1538  llvm::sys::path::append(P, "libclang_rt.cc_kext_ios.a");
1539  } else {
1540  llvm::sys::path::append(P, "libclang_rt.cc_kext.a");
1541  }
1542 
1543  // For now, allow missing resource libraries to support developers who may
1544  // not have compiler-rt checked out or integrated into their build.
1545  if (getVFS().exists(P))
1546  CmdArgs.push_back(Args.MakeArgString(P));
1547 }
1548 
1549 DerivedArgList *MachO::TranslateArgs(const DerivedArgList &Args,
1550  StringRef BoundArch,
1551  Action::OffloadKind) const {
1552  DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
1553  const OptTable &Opts = getDriver().getOpts();
1554 
1555  // FIXME: We really want to get out of the tool chain level argument
1556  // translation business, as it makes the driver functionality much
1557  // more opaque. For now, we follow gcc closely solely for the
1558  // purpose of easily achieving feature parity & testability. Once we
1559  // have something that works, we should reevaluate each translation
1560  // and try to push it down into tool specific logic.
1561 
1562  for (Arg *A : Args) {
1563  if (A->getOption().matches(options::OPT_Xarch__)) {
1564  // Skip this argument unless the architecture matches either the toolchain
1565  // triple arch, or the arch being bound.
1566  llvm::Triple::ArchType XarchArch =
1568  if (!(XarchArch == getArch() ||
1569  (!BoundArch.empty() &&
1570  XarchArch ==
1572  continue;
1573 
1574  Arg *OriginalArg = A;
1575  unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
1576  unsigned Prev = Index;
1577  std::unique_ptr<Arg> XarchArg(Opts.ParseOneArg(Args, Index));
1578 
1579  // If the argument parsing failed or more than one argument was
1580  // consumed, the -Xarch_ argument's parameter tried to consume
1581  // extra arguments. Emit an error and ignore.
1582  //
1583  // We also want to disallow any options which would alter the
1584  // driver behavior; that isn't going to work in our model. We
1585  // use isDriverOption() as an approximation, although things
1586  // like -O4 are going to slip through.
1587  if (!XarchArg || Index > Prev + 1) {
1588  getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
1589  << A->getAsString(Args);
1590  continue;
1591  } else if (XarchArg->getOption().hasFlag(options::DriverOption)) {
1592  getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
1593  << A->getAsString(Args);
1594  continue;
1595  }
1596 
1597  XarchArg->setBaseArg(A);
1598 
1599  A = XarchArg.release();
1600  DAL->AddSynthesizedArg(A);
1601 
1602  // Linker input arguments require custom handling. The problem is that we
1603  // have already constructed the phase actions, so we can not treat them as
1604  // "input arguments".
1605  if (A->getOption().hasFlag(options::LinkerInput)) {
1606  // Convert the argument into individual Zlinker_input_args.
1607  for (const char *Value : A->getValues()) {
1608  DAL->AddSeparateArg(
1609  OriginalArg, Opts.getOption(options::OPT_Zlinker_input), Value);
1610  }
1611  continue;
1612  }
1613  }
1614 
1615  // Sob. These is strictly gcc compatible for the time being. Apple
1616  // gcc translates options twice, which means that self-expanding
1617  // options add duplicates.
1618  switch ((options::ID)A->getOption().getID()) {
1619  default:
1620  DAL->append(A);
1621  break;
1622 
1623  case options::OPT_mkernel:
1624  case options::OPT_fapple_kext:
1625  DAL->append(A);
1626  DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
1627  break;
1628 
1629  case options::OPT_dependency_file:
1630  DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF), A->getValue());
1631  break;
1632 
1633  case options::OPT_gfull:
1634  DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
1635  DAL->AddFlagArg(
1636  A, Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
1637  break;
1638 
1639  case options::OPT_gused:
1640  DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
1641  DAL->AddFlagArg(
1642  A, Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
1643  break;
1644 
1645  case options::OPT_shared:
1646  DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
1647  break;
1648 
1649  case options::OPT_fconstant_cfstrings:
1650  DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
1651  break;
1652 
1653  case options::OPT_fno_constant_cfstrings:
1654  DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
1655  break;
1656 
1657  case options::OPT_Wnonportable_cfstrings:
1658  DAL->AddFlagArg(A,
1659  Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
1660  break;
1661 
1662  case options::OPT_Wno_nonportable_cfstrings:
1663  DAL->AddFlagArg(
1664  A, Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
1665  break;
1666 
1667  case options::OPT_fpascal_strings:
1668  DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
1669  break;
1670 
1671  case options::OPT_fno_pascal_strings:
1672  DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
1673  break;
1674  }
1675  }
1676 
1677  if (getTriple().getArch() == llvm::Triple::x86 ||
1678  getTriple().getArch() == llvm::Triple::x86_64)
1679  if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
1680  DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_mtune_EQ),
1681  "core2");
1682 
1683  // Add the arch options based on the particular spelling of -arch, to match
1684  // how the driver driver works.
1685  if (!BoundArch.empty()) {
1686  StringRef Name = BoundArch;
1687  const Option MCpu = Opts.getOption(options::OPT_mcpu_EQ);
1688  const Option MArch = Opts.getOption(clang::driver::options::OPT_march_EQ);
1689 
1690  // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
1691  // which defines the list of which architectures we accept.
1692  if (Name == "ppc")
1693  ;
1694  else if (Name == "ppc601")
1695  DAL->AddJoinedArg(nullptr, MCpu, "601");
1696  else if (Name == "ppc603")
1697  DAL->AddJoinedArg(nullptr, MCpu, "603");
1698  else if (Name == "ppc604")
1699  DAL->AddJoinedArg(nullptr, MCpu, "604");
1700  else if (Name == "ppc604e")
1701  DAL->AddJoinedArg(nullptr, MCpu, "604e");
1702  else if (Name == "ppc750")
1703  DAL->AddJoinedArg(nullptr, MCpu, "750");
1704  else if (Name == "ppc7400")
1705  DAL->AddJoinedArg(nullptr, MCpu, "7400");
1706  else if (Name == "ppc7450")
1707  DAL->AddJoinedArg(nullptr, MCpu, "7450");
1708  else if (Name == "ppc970")
1709  DAL->AddJoinedArg(nullptr, MCpu, "970");
1710 
1711  else if (Name == "ppc64" || Name == "ppc64le")
1712  DAL->AddFlagArg(nullptr, Opts.getOption(options::OPT_m64));
1713 
1714  else if (Name == "i386")
1715  ;
1716  else if (Name == "i486")
1717  DAL->AddJoinedArg(nullptr, MArch, "i486");
1718  else if (Name == "i586")
1719  DAL->AddJoinedArg(nullptr, MArch, "i586");
1720  else if (Name == "i686")
1721  DAL->AddJoinedArg(nullptr, MArch, "i686");
1722  else if (Name == "pentium")
1723  DAL->AddJoinedArg(nullptr, MArch, "pentium");
1724  else if (Name == "pentium2")
1725  DAL->AddJoinedArg(nullptr, MArch, "pentium2");
1726  else if (Name == "pentpro")
1727  DAL->AddJoinedArg(nullptr, MArch, "pentiumpro");
1728  else if (Name == "pentIIm3")
1729  DAL->AddJoinedArg(nullptr, MArch, "pentium2");
1730 
1731  else if (Name == "x86_64")
1732  DAL->AddFlagArg(nullptr, Opts.getOption(options::OPT_m64));
1733  else if (Name == "x86_64h") {
1734  DAL->AddFlagArg(nullptr, Opts.getOption(options::OPT_m64));
1735  DAL->AddJoinedArg(nullptr, MArch, "x86_64h");
1736  }
1737 
1738  else if (Name == "arm")
1739  DAL->AddJoinedArg(nullptr, MArch, "armv4t");
1740  else if (Name == "armv4t")
1741  DAL->AddJoinedArg(nullptr, MArch, "armv4t");
1742  else if (Name == "armv5")
1743  DAL->AddJoinedArg(nullptr, MArch, "armv5tej");
1744  else if (Name == "xscale")
1745  DAL->AddJoinedArg(nullptr, MArch, "xscale");
1746  else if (Name == "armv6")
1747  DAL->AddJoinedArg(nullptr, MArch, "armv6k");
1748  else if (Name == "armv6m")
1749  DAL->AddJoinedArg(nullptr, MArch, "armv6m");
1750  else if (Name == "armv7")
1751  DAL->AddJoinedArg(nullptr, MArch, "armv7a");
1752  else if (Name == "armv7em")
1753  DAL->AddJoinedArg(nullptr, MArch, "armv7em");
1754  else if (Name == "armv7k")
1755  DAL->AddJoinedArg(nullptr, MArch, "armv7k");
1756  else if (Name == "armv7m")
1757  DAL->AddJoinedArg(nullptr, MArch, "armv7m");
1758  else if (Name == "armv7s")
1759  DAL->AddJoinedArg(nullptr, MArch, "armv7s");
1760  }
1761 
1762  return DAL;
1763 }
1764 
1765 void MachO::AddLinkRuntimeLibArgs(const ArgList &Args,
1766  ArgStringList &CmdArgs) const {
1767  // Embedded targets are simple at the moment, not supporting sanitizers and
1768  // with different libraries for each member of the product { static, PIC } x
1769  // { hard-float, soft-float }
1770  llvm::SmallString<32> CompilerRT = StringRef("libclang_rt.");
1771  CompilerRT +=
1773  ? "hard"
1774  : "soft";
1775  CompilerRT += Args.hasArg(options::OPT_fPIC) ? "_pic.a" : "_static.a";
1776 
1777  AddLinkRuntimeLib(Args, CmdArgs, CompilerRT, RLO_IsEmbedded);
1778 }
1779 
1781  llvm::Triple::OSType OS;
1782 
1783  switch (TargetPlatform) {
1784  case MacOS: // Earlier than 10.13.
1785  OS = llvm::Triple::MacOSX;
1786  break;
1787  case IPhoneOS:
1788  case IPhoneOSSimulator:
1789  OS = llvm::Triple::IOS;
1790  break;
1791  case TvOS:
1792  case TvOSSimulator: // Earlier than 11.0.
1793  OS = llvm::Triple::TvOS;
1794  break;
1795  case WatchOS:
1796  case WatchOSSimulator: // Earlier than 4.0.
1797  OS = llvm::Triple::WatchOS;
1798  break;
1799  }
1800 
1802 }
1803 
1804 void Darwin::addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
1805  llvm::opt::ArgStringList &CC1Args,
1806  Action::OffloadKind DeviceOffloadKind) const {
1808  CC1Args.push_back("-faligned-alloc-unavailable");
1809 }
1810 
1811 DerivedArgList *
1812 Darwin::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
1813  Action::OffloadKind DeviceOffloadKind) const {
1814  // First get the generic Apple args, before moving onto Darwin-specific ones.
1815  DerivedArgList *DAL =
1816  MachO::TranslateArgs(Args, BoundArch, DeviceOffloadKind);
1817  const OptTable &Opts = getDriver().getOpts();
1818 
1819  // If no architecture is bound, none of the translations here are relevant.
1820  if (BoundArch.empty())
1821  return DAL;
1822 
1823  // Add an explicit version min argument for the deployment target. We do this
1824  // after argument translation because -Xarch_ arguments may add a version min
1825  // argument.
1826  AddDeploymentTarget(*DAL);
1827 
1828  // For iOS 6, undo the translation to add -static for -mkernel/-fapple-kext.
1829  // FIXME: It would be far better to avoid inserting those -static arguments,
1830  // but we can't check the deployment target in the translation code until
1831  // it is set here.
1832  if (isTargetWatchOSBased() ||
1833  (isTargetIOSBased() && !isIPhoneOSVersionLT(6, 0))) {
1834  for (ArgList::iterator it = DAL->begin(), ie = DAL->end(); it != ie; ) {
1835  Arg *A = *it;
1836  ++it;
1837  if (A->getOption().getID() != options::OPT_mkernel &&
1838  A->getOption().getID() != options::OPT_fapple_kext)
1839  continue;
1840  assert(it != ie && "unexpected argument translation");
1841  A = *it;
1842  assert(A->getOption().getID() == options::OPT_static &&
1843  "missing expected -static argument");
1844  *it = nullptr;
1845  ++it;
1846  }
1847  }
1848 
1849  if (!Args.getLastArg(options::OPT_stdlib_EQ) &&
1851  DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_stdlib_EQ),
1852  "libc++");
1853 
1854  // Validate the C++ standard library choice.
1856  if (Type == ToolChain::CST_Libcxx) {
1857  // Check whether the target provides libc++.
1858  StringRef where;
1859 
1860  // Complain about targeting iOS < 5.0 in any way.
1861  if (isTargetIOSBased() && isIPhoneOSVersionLT(5, 0))
1862  where = "iOS 5.0";
1863 
1864  if (where != StringRef()) {
1865  getDriver().Diag(clang::diag::err_drv_invalid_libcxx_deployment) << where;
1866  }
1867  }
1868 
1869  auto Arch = tools::darwin::getArchTypeForMachOArchName(BoundArch);
1870  if ((Arch == llvm::Triple::arm || Arch == llvm::Triple::thumb)) {
1871  if (Args.hasFlag(options::OPT_fomit_frame_pointer,
1872  options::OPT_fno_omit_frame_pointer, false))
1873  getDriver().Diag(clang::diag::warn_drv_unsupported_opt_for_target)
1874  << "-fomit-frame-pointer" << BoundArch;
1875  }
1876 
1877  return DAL;
1878 }
1879 
1880 bool MachO::IsUnwindTablesDefault(const ArgList &Args) const {
1881  // Unwind tables are not emitted if -fno-exceptions is supplied (except when
1882  // targeting x86_64).
1883  return getArch() == llvm::Triple::x86_64 ||
1884  (!UseSjLjExceptions(Args) &&
1885  Args.hasFlag(options::OPT_fexceptions, options::OPT_fno_exceptions,
1886  true));
1887 }
1888 
1890  if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
1891  return S[0] != '\0';
1892  return false;
1893 }
1894 
1895 bool Darwin::UseSjLjExceptions(const ArgList &Args) const {
1896  // Darwin uses SjLj exceptions on ARM.
1897  if (getTriple().getArch() != llvm::Triple::arm &&
1898  getTriple().getArch() != llvm::Triple::thumb)
1899  return false;
1900 
1901  // Only watchOS uses the new DWARF/Compact unwinding method.
1902  llvm::Triple Triple(ComputeLLVMTriple(Args));
1903  return !Triple.isWatchABI();
1904 }
1905 
1907  assert(TargetInitialized && "Target not initialized!");
1908  if (isTargetIPhoneOS() && isIPhoneOSVersionLT(6, 0))
1909  return false;
1910  return true;
1911 }
1912 
1913 bool MachO::isPICDefault() const { return true; }
1914 
1915 bool MachO::isPIEDefault() const { return false; }
1916 
1918  return (getArch() == llvm::Triple::x86_64 ||
1919  getArch() == llvm::Triple::aarch64);
1920 }
1921 
1923  // Profiling instrumentation is only supported on x86.
1924  return getArch() == llvm::Triple::x86 || getArch() == llvm::Triple::x86_64;
1925 }
1926 
1927 void Darwin::addMinVersionArgs(const ArgList &Args,
1928  ArgStringList &CmdArgs) const {
1930 
1931  if (isTargetWatchOS())
1932  CmdArgs.push_back("-watchos_version_min");
1933  else if (isTargetWatchOSSimulator())
1934  CmdArgs.push_back("-watchos_simulator_version_min");
1935  else if (isTargetTvOS())
1936  CmdArgs.push_back("-tvos_version_min");
1937  else if (isTargetTvOSSimulator())
1938  CmdArgs.push_back("-tvos_simulator_version_min");
1939  else if (isTargetIOSSimulator())
1940  CmdArgs.push_back("-ios_simulator_version_min");
1941  else if (isTargetIOSBased())
1942  CmdArgs.push_back("-iphoneos_version_min");
1943  else {
1944  assert(isTargetMacOS() && "unexpected target");
1945  CmdArgs.push_back("-macosx_version_min");
1946  }
1947 
1948  CmdArgs.push_back(Args.MakeArgString(TargetVersion.getAsString()));
1949 }
1950 
1951 void Darwin::addStartObjectFileArgs(const ArgList &Args,
1952  ArgStringList &CmdArgs) const {
1953  // Derived from startfile spec.
1954  if (Args.hasArg(options::OPT_dynamiclib)) {
1955  // Derived from darwin_dylib1 spec.
1956  if (isTargetWatchOSBased()) {
1957  ; // watchOS does not need dylib1.o.
1958  } else if (isTargetIOSSimulator()) {
1959  ; // iOS simulator does not need dylib1.o.
1960  } else if (isTargetIPhoneOS()) {
1961  if (isIPhoneOSVersionLT(3, 1))
1962  CmdArgs.push_back("-ldylib1.o");
1963  } else {
1964  if (isMacosxVersionLT(10, 5))
1965  CmdArgs.push_back("-ldylib1.o");
1966  else if (isMacosxVersionLT(10, 6))
1967  CmdArgs.push_back("-ldylib1.10.5.o");
1968  }
1969  } else {
1970  if (Args.hasArg(options::OPT_bundle)) {
1971  if (!Args.hasArg(options::OPT_static)) {
1972  // Derived from darwin_bundle1 spec.
1973  if (isTargetWatchOSBased()) {
1974  ; // watchOS does not need bundle1.o.
1975  } else if (isTargetIOSSimulator()) {
1976  ; // iOS simulator does not need bundle1.o.
1977  } else if (isTargetIPhoneOS()) {
1978  if (isIPhoneOSVersionLT(3, 1))
1979  CmdArgs.push_back("-lbundle1.o");
1980  } else {
1981  if (isMacosxVersionLT(10, 6))
1982  CmdArgs.push_back("-lbundle1.o");
1983  }
1984  }
1985  } else {
1986  if (Args.hasArg(options::OPT_pg) && SupportsProfiling()) {
1987  if (Args.hasArg(options::OPT_static) ||
1988  Args.hasArg(options::OPT_object) ||
1989  Args.hasArg(options::OPT_preload)) {
1990  CmdArgs.push_back("-lgcrt0.o");
1991  } else {
1992  CmdArgs.push_back("-lgcrt1.o");
1993 
1994  // darwin_crt2 spec is empty.
1995  }
1996  // By default on OS X 10.8 and later, we don't link with a crt1.o
1997  // file and the linker knows to use _main as the entry point. But,
1998  // when compiling with -pg, we need to link with the gcrt1.o file,
1999  // so pass the -no_new_main option to tell the linker to use the
2000  // "start" symbol as the entry point.
2001  if (isTargetMacOS() && !isMacosxVersionLT(10, 8))
2002  CmdArgs.push_back("-no_new_main");
2003  } else {
2004  if (Args.hasArg(options::OPT_static) ||
2005  Args.hasArg(options::OPT_object) ||
2006  Args.hasArg(options::OPT_preload)) {
2007  CmdArgs.push_back("-lcrt0.o");
2008  } else {
2009  // Derived from darwin_crt1 spec.
2010  if (isTargetWatchOSBased()) {
2011  ; // watchOS does not need crt1.o.
2012  } else if (isTargetIOSSimulator()) {
2013  ; // iOS simulator does not need crt1.o.
2014  } else if (isTargetIPhoneOS()) {
2015  if (getArch() == llvm::Triple::aarch64)
2016  ; // iOS does not need any crt1 files for arm64
2017  else if (isIPhoneOSVersionLT(3, 1))
2018  CmdArgs.push_back("-lcrt1.o");
2019  else if (isIPhoneOSVersionLT(6, 0))
2020  CmdArgs.push_back("-lcrt1.3.1.o");
2021  } else {
2022  if (isMacosxVersionLT(10, 5))
2023  CmdArgs.push_back("-lcrt1.o");
2024  else if (isMacosxVersionLT(10, 6))
2025  CmdArgs.push_back("-lcrt1.10.5.o");
2026  else if (isMacosxVersionLT(10, 8))
2027  CmdArgs.push_back("-lcrt1.10.6.o");
2028 
2029  // darwin_crt2 spec is empty.
2030  }
2031  }
2032  }
2033  }
2034  }
2035 
2036  if (!isTargetIPhoneOS() && Args.hasArg(options::OPT_shared_libgcc) &&
2037  !isTargetWatchOS() &&
2038  isMacosxVersionLT(10, 5)) {
2039  const char *Str = Args.MakeArgString(GetFilePath("crt3.o"));
2040  CmdArgs.push_back(Str);
2041  }
2042 }
2043 
2044 bool Darwin::SupportsObjCGC() const { return isTargetMacOS(); }
2045 
2046 void Darwin::CheckObjCARC() const {
2048  (isTargetMacOS() && !isMacosxVersionLT(10, 6)))
2049  return;
2050  getDriver().Diag(diag::err_arc_unsupported_on_toolchain);
2051 }
2052 
2054  const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
2056  Res |= SanitizerKind::Address;
2057  Res |= SanitizerKind::Leak;
2058  Res |= SanitizerKind::Fuzzer;
2059  Res |= SanitizerKind::FuzzerNoLink;
2060  Res |= SanitizerKind::Function;
2061  if (isTargetMacOS()) {
2062  if (!isMacosxVersionLT(10, 9))
2063  Res |= SanitizerKind::Vptr;
2064  Res |= SanitizerKind::SafeStack;
2065  if (IsX86_64)
2066  Res |= SanitizerKind::Thread;
2067  } else if (isTargetIOSSimulator() || isTargetTvOSSimulator()) {
2068  if (IsX86_64)
2069  Res |= SanitizerKind::Thread;
2070  }
2071  return Res;
2072 }
2073 
2074 void Darwin::printVerboseInfo(raw_ostream &OS) const {
2075  CudaInstallation.print(OS);
2076 }
StringRef getSysRoot() const
Returns the sysroot path.
static bool GetReleaseVersion(StringRef Str, unsigned &Major, unsigned &Minor, unsigned &Micro, bool &HadExtra)
GetReleaseVersion - Parse (([0-9]+)(.
Definition: Driver.cpp:3963
StringRef getMachOArchName(const llvm::opt::ArgList &Args) const
Get the "MachO" arch name for a particular compiler invocation.
Definition: Darwin.cpp:760
virtual std::string ComputeLLVMTriple(const llvm::opt::ArgList &Args, types::ID InputType=types::TY_INVALID) const
ComputeLLVMTriple - Return the LLVM target triple to use, after taking command line arguments into ac...
Definition: ToolChain.cpp:455
bool SupportsObjCGC() const override
Does this tool chain support Objective-C garbage collection.
Definition: Darwin.cpp:2044
bool SupportsEmbeddedBitcode() const override
SupportsEmbeddedBitcode - Does this tool chain support embedded bitcode.
Definition: Darwin.cpp:1906
bool isMacosxVersionLT(unsigned V0, unsigned V1=0, unsigned V2=0) const
Definition: Darwin.h:399
Tool * buildAssembler() const override
Definition: Darwin.cpp:831
Represents a version number in the form major[.minor[.subminor[.build]]].
Definition: VersionTuple.h:26
std::string GetTemporaryPath(StringRef Prefix, StringRef Suffix) const
GetTemporaryPath - Return the pathname of a temporary file to use as part of compilation; the file wi...
Definition: Driver.cpp:3773
Link the library in even if it can&#39;t be found in the VFS.
Definition: Darwin.h:178
Use the embedded runtime from the macho_embedded directory.
Definition: Darwin.h:181
const char * getTypeTempSuffix(ID Id, bool CLMode=false)
getTypeTempSuffix - Return the suffix to use when creating a temp file of this type, or null if unspecified.
Definition: Types.cpp:55
bool UseDwarfDebugFlags() const override
UseDwarfDebugFlags - Embed the compile options to clang into the Dwarf compile unit information...
Definition: Darwin.cpp:1889
StringRef P
bool isUsingLTO() const
Returns true if we are performing any kind of LTO.
Definition: Driver.h:490
Defines types useful for describing an Objective-C runtime.
CXXStdlibType GetDefaultCXXStdlibType() const override
Definition: Darwin.cpp:682
The base class of the type hierarchy.
Definition: Type.h:1300
bool HasNativeLLVMSupport() const override
HasNativeLTOLinker - Check whether the linker and related tools have native LLVM support.
Definition: Darwin.cpp:680
bool embedBitcodeEnabled() const
Definition: Driver.h:313
DiagnosticBuilder Diag(unsigned DiagID) const
Definition: Driver.h:116
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs...
Definition: Darwin.cpp:421
void addProfileRTLibs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const override
Add any profiling runtime libraries that are needed.
Definition: Darwin.cpp:1008
FloatABI getARMFloatABI(const ToolChain &TC, const llvm::opt::ArgList &Args)
CudaInstallationDetector CudaInstallation
Definition: Darwin.h:284
bool TargetInitialized
Whether the information on the target has been initialized.
Definition: Darwin.h:267
void print(raw_ostream &OS) const
Print information about the detected CUDA installation.
Definition: Cuda.cpp:220
bool hasBlocksRuntime() const override
Darwin provides a blocks runtime starting in MacOS X 10.6 and iOS 3.2.
Definition: Darwin.cpp:704
types::ID LookupTypeForExtension(StringRef Ext) const override
LookupTypeForExtension - Return the default language type to use for the given extension.
Definition: Darwin.cpp:670
&#39;macosx-fragile&#39; is the Apple-provided NeXT-derived runtime on Mac OS X platforms that use the fragil...
Definition: ObjCRuntime.h:37
const char * getFilename() const
Definition: InputInfo.h:84
path_list & getProgramPaths()
Definition: ToolChain.h:197
bool isPIEDefault() const override
Test whether this toolchain defaults to PIE.
Definition: Darwin.cpp:1915
bool embedBitcodeMarkerOnly() const
Definition: Driver.h:315
ActionList & getInputs()
Definition: Action.h:134
bool IsUnwindTablesDefault(const llvm::opt::ArgList &Args) const override
IsUnwindTablesDefault - Does this tool chain use -funwind-tables by default.
Definition: Darwin.cpp:1880
StringRef getPlatformFamily() const
Definition: Darwin.cpp:939
std::string Dir
The path the driver executable was in, as invoked from the command line.
Definition: Driver.h:127
Darwin - The base Darwin tool chain.
Definition: Darwin.h:260
void AddLinkRuntimeLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const override
Add the linker arguments to link the compiler runtime library.
Definition: Darwin.cpp:1053
ObjCRuntime getDefaultObjCRuntime(bool isNonFragile) const override
Darwin provides an ARC runtime starting in MacOS X 10.7 and iOS 5.0.
Definition: Darwin.cpp:693
bool isPICDefault() const override
Test whether this toolchain defaults to PIC.
Definition: Darwin.cpp:1913
Action - Represent an abstract compilation step to perform.
Definition: Action.h:45
static bool needsProfileRT(const llvm::opt::ArgList &Args)
needsProfileRT - returns true if instrumentation profile is on.
Definition: ToolChain.cpp:349
llvm::opt::DerivedArgList * TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch, Action::OffloadKind DeviceOffloadKind) const override
TranslateArgs - Create a new derived argument list for any argument translations this ToolChain may w...
Definition: Darwin.cpp:1549
Tool * getTool(Action::ActionClass AC) const override
Definition: Darwin.cpp:810
InputInfo - Wrapper for information about an input source.
Definition: InputInfo.h:23
static bool hasExportSymbolDirective(const ArgList &Args)
Check if the link command contains a symbol export directive.
Definition: Darwin.cpp:990
std::string GetFilePath(const char *Name) const
Definition: ToolChain.cpp:371
vfs::FileSystem & getVFS() const
Definition: ToolChain.cpp:85
types::ID getType() const
Definition: Action.h:132
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition: Driver.h:65
types::ID getType() const
Definition: InputInfo.h:78
SanitizerMask getSupportedSanitizers() const override
Return sanitizers which are available in this toolchain.
Definition: Darwin.cpp:2053
bool isTargetInitialized() const
Definition: Darwin.h:386
void AddMachOArch(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
Definition: Darwin.cpp:154
bool isPICDefaultForced() const override
Tests whether this toolchain forces its default for PIC, PIE or non-PIC.
Definition: Darwin.cpp:1917
bool addOpenMPRuntime(llvm::opt::ArgStringList &CmdArgs, const ToolChain &TC, const llvm::opt::ArgList &Args, bool IsOffloadingHost=false, bool GompNeedsRT=false)
Returns true, if an OpenMP runtime has been added.
static bool shouldLinkerNotDedup(bool IsLinkerOnlyAction, const ArgList &Args)
Pass -no_deduplicate to ld64 under certain conditions:
Definition: Darwin.cpp:185
Darwin(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
Darwin - Darwin tool chain for i386 and x86_64.
Definition: Darwin.cpp:666
&#39;watchos&#39; is a variant of iOS for Apple&#39;s watchOS.
Definition: ObjCRuntime.h:46
void printVerboseInfo(raw_ostream &OS) const override
Dispatch to the specific toolchain for verbose printing.
Definition: Darwin.cpp:2074
Defines a function that returns the minimum OS versions supporting C++17&#39;s aligned allocation functio...
bool requiresMinimalRuntime() const
Definition: SanitizerArgs.h:65
void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, Action::OffloadKind DeviceOffloadKind) const override
Add options that need to be passed to cc1 for this target.
Definition: Darwin.cpp:1804
ActionClass getKind() const
Definition: Action.h:131
bool isTargetWatchOSBased() const
Definition: Darwin.h:376
&#39;macosx&#39; is the Apple-provided NeXT-derived runtime on Mac OS X platforms that use the non-fragile AB...
Definition: ObjCRuntime.h:32
llvm::StringRef getAsString(SyncScope S)
Definition: SyncScope.h:51
static void addExportedSymbol(ArgStringList &CmdArgs, const char *Symbol)
Add an export directive for Symbol to the link command.
Definition: Darwin.cpp:1003
const FunctionProtoType * T
virtual bool SupportsEmbeddedBitcode() const
SupportsEmbeddedBitcode - Does this tool chain support embedded bitcode.
Definition: ToolChain.h:400
VersionTuple TargetVersion
The OS version we are targeting.
Definition: Darwin.h:282
static const char * ArmMachOArchNameCPU(StringRef CPU)
Definition: Darwin.cpp:741
bool UseSjLjExceptions(const llvm::opt::ArgList &Args) const override
UseSjLjExceptions - Does this tool chain use SjLj exceptions.
Definition: Darwin.cpp:1895
void addCommand(std::unique_ptr< Command > C)
Definition: Compilation.h:189
void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const override
Add arguments to use system-specific CUDA includes.
Definition: Darwin.cpp:715
bool isObjCAutoRefCount(const llvm::opt::ArgList &Args)
bool isTargetIOSSimulator() const
Definition: Darwin.h:340
llvm::Triple::ArchType getArchTypeForMachOArchName(StringRef Str)
Definition: Darwin.cpp:34
unsigned GetDefaultDwarfVersion() const override
Definition: Darwin.cpp:891
virtual void addMinVersionArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
Definition: Darwin.h:165
llvm::Triple::ArchType getArch() const
Definition: ToolChain.h:175
void AddCudaIncludeArgs(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args) const
Definition: Cuda.cpp:179
Link the library in before any others.
Definition: Darwin.h:187
static std::string getSystemOrSDKMacOSVersion(StringRef MacOSSDKVersion)
Returns the most appropriate macOS target version for the current process.
Definition: Darwin.cpp:1158
#define false
Definition: stdbool.h:33
llvm::opt::DerivedArgList * TranslateArgs(const llvm::opt::DerivedArgList &Args, StringRef BoundArch, Action::OffloadKind DeviceOffloadKind) const override
TranslateArgs - Create a new derived argument list for any argument translations this ToolChain may w...
Definition: Darwin.cpp:1812
bool SupportsProfiling() const override
SupportsProfiling - Does this tool chain support -pg.
Definition: Darwin.cpp:1922
const llvm::opt::DerivedArgList & getArgs() const
Definition: Compilation.h:170
VersionTuple getTargetVersion() const
Definition: Darwin.h:388
const Driver & getDriver() const
Definition: ToolChain.h:164
void AddLinkRuntimeLib(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, StringRef DarwinLibName, RuntimeLinkOptions Opts=RuntimeLinkOptions()) const
Add a runtime library to the list of items to link.
Definition: Darwin.cpp:899
std::string ComputeEffectiveClangTriple(const llvm::opt::ArgList &Args, types::ID InputType) const override
ComputeEffectiveClangTriple - Return the Clang triple to use for this target, which may take into acc...
Definition: Darwin.cpp:786
bool isTargetIOSBased() const
Is the target either iOS or an iOS simulator?
Definition: Darwin.h:173
DarwinClang(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args)
Definition: Darwin.cpp:835
llvm::opt::Arg * getLastProfileUseArg(const llvm::opt::ArgList &Args)
VersionTuple alignedAllocMinVersion(llvm::Triple::OSType OS)
void addStartObjectFileArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const override
Definition: Darwin.cpp:1951
static const char * ArmMachOArchName(StringRef Arch)
Definition: Darwin.cpp:724
void AddLinkARCArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const override
Add the linker arguments to link the ARC runtime library.
Definition: Darwin.cpp:854
DarwinPlatformKind TargetPlatform
Definition: Darwin.h:279
Dataflow Directional Tag Classes.
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs...
Definition: Darwin.cpp:634
uint64_t SanitizerMask
Definition: Sanitizers.h:24
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs...
Definition: Darwin.cpp:614
static StringRef getSDKName(StringRef isysroot)
Definition: Darwin.cpp:956
void CheckObjCARC() const override
Complain if this tool chain doesn&#39;t support Objective-C ARC.
Definition: Darwin.cpp:2046
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs...
Definition: Darwin.cpp:84
The basic abstraction for the target Objective-C runtime.
Definition: ObjCRuntime.h:25
StringRef getOSLibraryNameSuffix() const
Definition: Darwin.cpp:969
void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const JobAction &JA)
virtual void AddLinkRuntimeLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const
Add the linker arguments to link the compiler runtime library.
Definition: Darwin.cpp:1765
unsigned getLTOParallelism(const llvm::opt::ArgList &Args, const Driver &D)
Tool - Information on a specific compilation tool.
Definition: Tool.h:34
void setTripleTypeForMachOArchName(llvm::Triple &T, StringRef Str)
Definition: Darwin.cpp:69
Defines the virtual file system interface vfs::FileSystem.
const SanitizerArgs & getSanitizerArgs() const
Definition: ToolChain.cpp:93
void addMinVersionArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const override
Definition: Darwin.cpp:1927
void AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const override
AddCXXStdlibLibArgs - Add the system specific linker arguments to use for the given C++ standard libr...
Definition: Darwin.cpp:1478
std::string getAsString() const
Retrieve a string representation of the version number.
&#39;ios&#39; is the Apple-provided NeXT-derived runtime on iOS or the iOS simulator; it is always non-fragil...
Definition: ObjCRuntime.h:42
bool exists(const Twine &Path)
Check whether a file exists. Provided for convenience.
StringRef getDefaultUniversalArchName() const
Provide the default architecture name (as expected by -arch) for this toolchain.
Definition: ToolChain.cpp:201
Compilation - A set of tasks to perform for a single driver invocation.
Definition: Compilation.h:34
const Driver & getDriver() const
Definition: Compilation.h:117
RuntimeLinkOptions
Options to control how a runtime library is linked.
Definition: Darwin.h:176
const llvm::Triple & getTriple() const
Definition: ToolChain.h:166
Tool * buildLinker() const override
Definition: Darwin.cpp:829
virtual SanitizerMask getSupportedSanitizers() const
Return sanitizers which are available in this toolchain.
Definition: ToolChain.cpp:730
ID lookupTypeForExtension(llvm::StringRef Ext)
lookupTypeForExtension - Lookup the type to use for the file extension Ext.
Definition: Types.cpp:177
virtual Tool * getTool(Action::ActionClass AC) const
Definition: ToolChain.cpp:260
Emit rpaths for as well as the resource directory.
Definition: Darwin.h:184
void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const override
Add warning options that need to be passed to cc1 for this target.
Definition: Darwin.cpp:839
bool isTargetWatchOSSimulator() const
Definition: Darwin.h:371
bool isFilename() const
Definition: InputInfo.h:76
const llvm::opt::OptTable & getOpts() const
Definition: Driver.h:280
virtual CXXStdlibType GetCXXStdlibType(const llvm::opt::ArgList &Args) const
Definition: ToolChain.cpp:599
const char * addTempFile(const char *Name)
addTempFile - Add a file to remove on exit, and returns its argument.
Definition: Compilation.h:216
RuntimeLibType GetRuntimeLibType(const llvm::opt::ArgList &Args) const override
Definition: Darwin.cpp:1041
bool hasNativeARC() const
Does this runtime natively provide the ARC entrypoints?
Definition: ObjCRuntime.h:160
static bool isObjCRuntimeLinked(const ArgList &Args)
Determine whether we are linking the ObjC runtime.
Definition: Darwin.cpp:413
bool isTargetTvOSSimulator() const
Definition: Darwin.h:356
bool isIPhoneOSVersionLT(unsigned V0, unsigned V1=0, unsigned V2=0) const
Definition: Darwin.h:393
bool hasSubscripting() const
Does this runtime directly support the subscripting methods?
Definition: ObjCRuntime.h:207
void setTarget(DarwinPlatformKind Platform, unsigned Major, unsigned Minor, unsigned Micro) const
Definition: Darwin.h:321
bool empty() const
Definition: Job.h:188
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs...
Definition: Darwin.cpp:592
bool isAlignedAllocationUnavailable() const
Return true if c++17 aligned allocation/deallocation functions are not implemented in the c++ standar...
Definition: Darwin.cpp:1780
void AddCCKextLibArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs) const override
AddCCKextLibArgs - Add the system specific linker arguments to use for kernel extensions (Darwin-spec...
Definition: Darwin.cpp:1523
ToolChain - Access to tools for a single platform.
Definition: ToolChain.h:72