Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Driver/ToolChains/Darwin.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- Darwin.cpp - Darwin Tool and ToolChain Implementations -*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "Darwin.h"
10
#include "Arch/ARM.h"
11
#include "CommonArgs.h"
12
#include "clang/Basic/AlignedAllocation.h"
13
#include "clang/Basic/ObjCRuntime.h"
14
#include "clang/Config/config.h"
15
#include "clang/Driver/Compilation.h"
16
#include "clang/Driver/Driver.h"
17
#include "clang/Driver/DriverDiagnostic.h"
18
#include "clang/Driver/Options.h"
19
#include "clang/Driver/SanitizerArgs.h"
20
#include "llvm/ADT/StringSwitch.h"
21
#include "llvm/Option/ArgList.h"
22
#include "llvm/Support/Path.h"
23
#include "llvm/Support/ScopedPrinter.h"
24
#include "llvm/Support/TargetParser.h"
25
#include "llvm/Support/VirtualFileSystem.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
76.1k
llvm::Triple::ArchType darwin::getArchTypeForMachOArchName(StringRef Str) {
35
76.1k
  // See arch(3) and llvm-gcc's driver-driver.c. We don't implement support for
36
76.1k
  // archs which Darwin doesn't use.
37
76.1k
38
76.1k
  // The matching this routine does is fairly pointless, since it is neither the
39
76.1k
  // complete architecture list, nor a reasonable subset. The problem is that
40
76.1k
  // historically the driver driver accepts this and also ties its -march=
41
76.1k
  // handling to the architecture name, so we need to be careful before removing
42
76.1k
  // support for it.
43
76.1k
44
76.1k
  // This code must be kept in sync with Clang's Darwin specific argument
45
76.1k
  // translation.
46
76.1k
47
76.1k
  return llvm::StringSwitch<llvm::Triple::ArchType>(Str)
48
76.1k
      .Cases("ppc", "ppc601", "ppc603", "ppc604", "ppc604e", llvm::Triple::ppc)
49
76.1k
      .Cases("ppc750", "ppc7400", "ppc7450", "ppc970", llvm::Triple::ppc)
50
76.1k
      .Case("ppc64", llvm::Triple::ppc64)
51
76.1k
      .Cases("i386", "i486", "i486SX", "i586", "i686", llvm::Triple::x86)
52
76.1k
      .Cases("pentium", "pentpro", "pentIIm3", "pentIIm5", "pentium4",
53
76.1k
             llvm::Triple::x86)
54
76.1k
      .Cases("x86_64", "x86_64h", llvm::Triple::x86_64)
55
76.1k
      // This is derived from the driver driver.
56
76.1k
      .Cases("arm", "armv4t", "armv5", "armv6", "armv6m", llvm::Triple::arm)
57
76.1k
      .Cases("armv7", "armv7em", "armv7k", "armv7m", llvm::Triple::arm)
58
76.1k
      .Cases("armv7s", "xscale", llvm::Triple::arm)
59
76.1k
      .Case("arm64", llvm::Triple::aarch64)
60
76.1k
      .Case("r600", llvm::Triple::r600)
61
76.1k
      .Case("amdgcn", llvm::Triple::amdgcn)
62
76.1k
      .Case("nvptx", llvm::Triple::nvptx)
63
76.1k
      .Case("nvptx64", llvm::Triple::nvptx64)
64
76.1k
      .Case("amdil", llvm::Triple::amdil)
65
76.1k
      .Case("spir", llvm::Triple::spir)
66
76.1k
      .Default(llvm::Triple::UnknownArch);
67
76.1k
}
68
69
36.3k
void darwin::setTripleTypeForMachOArchName(llvm::Triple &T, StringRef Str) {
70
36.3k
  const llvm::Triple::ArchType Arch = getArchTypeForMachOArchName(Str);
71
36.3k
  llvm::ARM::ArchKind ArchKind = llvm::ARM::parseArch(Str);
72
36.3k
  T.setArch(Arch);
73
36.3k
74
36.3k
  if (Str == "x86_64h")
75
1.37k
    T.setArchName(Str);
76
35.0k
  else if (ArchKind == llvm::ARM::ArchKind::ARMV6M ||
77
35.0k
           
ArchKind == llvm::ARM::ArchKind::ARMV7M34.6k
||
78
35.0k
           
ArchKind == llvm::ARM::ArchKind::ARMV7EM34.2k
) {
79
1.61k
    T.setOS(llvm::Triple::UnknownOS);
80
1.61k
    T.setObjectFormat(llvm::Triple::MachO);
81
1.61k
  }
82
36.3k
}
83
84
void darwin::Assembler::ConstructJob(Compilation &C, const JobAction &JA,
85
                                     const InputInfo &Output,
86
                                     const InputInfoList &Inputs,
87
                                     const ArgList &Args,
88
15
                                     const char *LinkingOutput) const {
89
15
  ArgStringList CmdArgs;
90
15
91
15
  assert(Inputs.size() == 1 && "Unexpected number of inputs.");
92
15
  const InputInfo &Input = Inputs[0];
93
15
94
15
  // Determine the original source input.
95
15
  const Action *SourceAction = &JA;
96
56
  while (SourceAction->getKind() != Action::InputClass) {
97
41
    assert(!SourceAction->getInputs().empty() && "unexpected root action!");
98
41
    SourceAction = SourceAction->getInputs()[0];
99
41
  }
100
15
101
15
  // If -fno-integrated-as is used add -Q to the darwin assembler driver to make
102
15
  // sure it runs its system assembler not clang's integrated assembler.
103
15
  // Applicable to darwin11+ and Xcode 4+.  darwin<10 lacked integrated-as.
104
15
  // FIXME: at run-time detect assembler capabilities or rely on version
105
15
  // information forwarded by -target-assembler-version.
106
15
  if (Args.hasArg(options::OPT_fno_integrated_as)) {
107
15
    const llvm::Triple &T(getToolChain().getTriple());
108
15
    if (!(T.isMacOSX() && 
T.isMacOSXVersionLT(10, 7)12
))
109
12
      CmdArgs.push_back("-Q");
110
15
  }
111
15
112
15
  // Forward -g, assuming we are dealing with an actual assembly file.
113
15
  if (SourceAction->getType() == types::TY_Asm ||
114
15
      
SourceAction->getType() == types::TY_PP_Asm13
) {
115
7
    if (Args.hasArg(options::OPT_gstabs))
116
0
      CmdArgs.push_back("--gstabs");
117
7
    else if (Args.hasArg(options::OPT_g_Group))
118
0
      CmdArgs.push_back("-g");
119
7
  }
120
15
121
15
  // Derived from asm spec.
122
15
  AddMachOArch(Args, CmdArgs);
123
15
124
15
  // Use -force_cpusubtype_ALL on x86 by default.
125
15
  if (getToolChain().getArch() == llvm::Triple::x86 ||
126
15
      
getToolChain().getArch() == llvm::Triple::x86_6412
||
127
15
      
Args.hasArg(options::OPT_force__cpusubtype__ALL)4
)
128
11
    CmdArgs.push_back("-force_cpusubtype_ALL");
129
15
130
15
  if (getToolChain().getArch() != llvm::Triple::x86_64 &&
131
15
      
(7
(7
(7
Args.hasArg(options::OPT_mkernel)7
||
132
7
         
Args.hasArg(options::OPT_fapple_kext)6
) &&
133
7
        
getMachOToolChain().isKernelStatic()1
) ||
134
7
       Args.hasArg(options::OPT_static)))
135
2
    CmdArgs.push_back("-static");
136
15
137
15
  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
138
15
139
15
  assert(Output.isFilename() && "Unexpected lipo output.");
140
15
  CmdArgs.push_back("-o");
141
15
  CmdArgs.push_back(Output.getFilename());
142
15
143
15
  assert(Input.isFilename() && "Invalid input.");
144
15
  CmdArgs.push_back(Input.getFilename());
145
15
146
15
  // asm_final spec is empty.
147
15
148
15
  const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("as"));
149
15
  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
150
15
}
151
152
0
void darwin::MachOTool::anchor() {}
153
154
void darwin::MachOTool::AddMachOArch(const ArgList &Args,
155
2.64k
                                     ArgStringList &CmdArgs) const {
156
2.64k
  StringRef ArchName = getMachOToolChain().getMachOArchName(Args);
157
2.64k
158
2.64k
  // Derived from darwin_arch spec.
159
2.64k
  CmdArgs.push_back("-arch");
160
2.64k
  CmdArgs.push_back(Args.MakeArgString(ArchName));
161
2.64k
162
2.64k
  // FIXME: Is this needed anymore?
163
2.64k
  if (ArchName == "arm")
164
0
    CmdArgs.push_back("-force_cpusubtype_ALL");
165
2.64k
}
166
167
13
bool darwin::Linker::NeedsTempPath(const InputInfoList &Inputs) const {
168
13
  // We only need to generate a temp path for LTO if we aren't compiling object
169
13
  // files. When compiling source files, we run 'dsymutil' after linking. We
170
13
  // don't run 'dsymutil' when compiling object files.
171
13
  for (const auto &Input : Inputs)
172
13
    if (Input.getType() != types::TY_Object)
173
13
      return true;
174
13
175
13
  
return false0
;
176
13
}
177
178
/// 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
2.61k
static bool shouldLinkerNotDedup(bool IsLinkerOnlyAction, const ArgList &Args) {
186
2.61k
  if (Arg *A = Args.getLastArg(options::OPT_O_Group)) {
187
2.03k
    if (A->getOption().matches(options::OPT_O0))
188
10
      return true;
189
2.02k
    if (A->getOption().matches(options::OPT_O))
190
2.01k
      return llvm::StringSwitch<bool>(A->getValue())
191
2.01k
                    .Case("1", true)
192
2.01k
                    .Default(false);
193
10
    return false; // OPT_Ofast & OPT_O4
194
10
  }
195
578
196
578
  if (!IsLinkerOnlyAction) // Implicit -O0 for compile+linker only.
197
412
    return true;
198
166
  return false;
199
166
}
200
201
void darwin::Linker::AddLinkArgs(Compilation &C, const ArgList &Args,
202
                                 ArgStringList &CmdArgs,
203
2.63k
                                 const InputInfoList &Inputs) const {
204
2.63k
  const Driver &D = getToolChain().getDriver();
205
2.63k
  const toolchains::MachO &MachOTC = getMachOToolChain();
206
2.63k
207
2.63k
  unsigned Version[5] = {0, 0, 0, 0, 0};
208
2.63k
  if (Arg *A = Args.getLastArg(options::OPT_mlinker_version_EQ)) {
209
2.63k
    if (!Driver::GetReleaseVersion(A->getValue(), Version))
210
3
      D.Diag(diag::err_drv_invalid_version_number) << A->getAsString(Args);
211
2.63k
  }
212
2.63k
213
2.63k
  // Newer linkers support -demangle. Pass it if supported and not disabled by
214
2.63k
  // the user.
215
2.63k
  if (Version[0] >= 100 && 
!Args.hasArg(options::OPT_Z_Xlinker__no_demangle)2.62k
)
216
2.62k
    CmdArgs.push_back("-demangle");
217
2.63k
218
2.63k
  if (Args.hasArg(options::OPT_rdynamic) && 
Version[0] >= 1372
)
219
1
    CmdArgs.push_back("-export_dynamic");
220
2.63k
221
2.63k
  // If we are using App Extension restrictions, pass a flag to the linker
222
2.63k
  // telling it that the compiled code has been audited.
223
2.63k
  if (Args.hasFlag(options::OPT_fapplication_extension,
224
2.63k
                   options::OPT_fno_application_extension, false))
225
55
    CmdArgs.push_back("-application_extension");
226
2.63k
227
2.63k
  if (D.isUsingLTO() && 
Version[0] >= 11613
&&
NeedsTempPath(Inputs)13
) {
228
13
    std::string TmpPathName;
229
13
    if (D.getLTOMode() == LTOK_Full) {
230
8
      // If we are using full LTO, then automatically create a temporary file
231
8
      // path for the linker to use, so that it's lifetime will extend past a
232
8
      // possible dsymutil step.
233
8
      TmpPathName =
234
8
          D.GetTemporaryPath("cc", types::getTypeTempSuffix(types::TY_Object));
235
8
    } else 
if (5
D.getLTOMode() == LTOK_Thin5
)
236
4
      // If we are using thin LTO, then create a directory instead.
237
4
      TmpPathName = D.GetTemporaryDirectory("thinlto");
238
13
239
13
    if (!TmpPathName.empty()) {
240
12
      auto *TmpPath = C.getArgs().MakeArgString(TmpPathName);
241
12
      C.addTempFile(TmpPath);
242
12
      CmdArgs.push_back("-object_path_lto");
243
12
      CmdArgs.push_back(TmpPath);
244
12
    }
245
13
  }
246
2.63k
247
2.63k
  // Use -lto_library option to specify the libLTO.dylib path. Try to find
248
2.63k
  // it in clang installed libraries. ld64 will only look at this argument
249
2.63k
  // when it actually uses LTO, so libLTO.dylib only needs to exist at link
250
2.63k
  // time if ld64 decides that it needs to use LTO.
251
2.63k
  // Since this is passed unconditionally, ld64 will never look for libLTO.dylib
252
2.63k
  // next to it. That's ok since ld64 using a libLTO.dylib not matching the
253
2.63k
  // clang version won't work anyways.
254
2.63k
  if (Version[0] >= 133) {
255
2.62k
    // Search for libLTO in <InstalledDir>/../lib/libLTO.dylib
256
2.62k
    StringRef P = llvm::sys::path::parent_path(D.Dir);
257
2.62k
    SmallString<128> LibLTOPath(P);
258
2.62k
    llvm::sys::path::append(LibLTOPath, "lib");
259
2.62k
    llvm::sys::path::append(LibLTOPath, "libLTO.dylib");
260
2.62k
    CmdArgs.push_back("-lto_library");
261
2.62k
    CmdArgs.push_back(C.getArgs().MakeArgString(LibLTOPath));
262
2.62k
  }
263
2.63k
264
2.63k
  // ld64 version 262 and above run the deduplicate pass by default.
265
2.63k
  if (Version[0] >= 262 && 
shouldLinkerNotDedup(C.getJobs().empty(), Args)2.61k
)
266
426
    CmdArgs.push_back("-no_deduplicate");
267
2.63k
268
2.63k
  // Derived from the "link" spec.
269
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_static);
270
2.63k
  if (!Args.hasArg(options::OPT_static))
271
2.62k
    CmdArgs.push_back("-dynamic");
272
2.63k
  if (Args.hasArg(options::OPT_fgnu_runtime)) {
273
0
    // FIXME: gcc replaces -lobjc in forward args with -lobjc-gnu
274
0
    // here. How do we wish to handle such things?
275
0
  }
276
2.63k
277
2.63k
  if (!Args.hasArg(options::OPT_dynamiclib)) {
278
2.58k
    AddMachOArch(Args, CmdArgs);
279
2.58k
    // FIXME: Why do this only on this path?
280
2.58k
    Args.AddLastArg(CmdArgs, options::OPT_force__cpusubtype__ALL);
281
2.58k
282
2.58k
    Args.AddLastArg(CmdArgs, options::OPT_bundle);
283
2.58k
    Args.AddAllArgs(CmdArgs, options::OPT_bundle__loader);
284
2.58k
    Args.AddAllArgs(CmdArgs, options::OPT_client__name);
285
2.58k
286
2.58k
    Arg *A;
287
2.58k
    if ((A = Args.getLastArg(options::OPT_compatibility__version)) ||
288
2.58k
        (A = Args.getLastArg(options::OPT_current__version)) ||
289
2.58k
        (A = Args.getLastArg(options::OPT_install__name)))
290
0
      D.Diag(diag::err_drv_argument_only_allowed_with) << A->getAsString(Args)
291
0
                                                       << "-dynamiclib";
292
2.58k
293
2.58k
    Args.AddLastArg(CmdArgs, options::OPT_force__flat__namespace);
294
2.58k
    Args.AddLastArg(CmdArgs, options::OPT_keep__private__externs);
295
2.58k
    Args.AddLastArg(CmdArgs, options::OPT_private__bundle);
296
2.58k
  } else {
297
48
    CmdArgs.push_back("-dylib");
298
48
299
48
    Arg *A;
300
48
    if ((A = Args.getLastArg(options::OPT_bundle)) ||
301
48
        (A = Args.getLastArg(options::OPT_bundle__loader)) ||
302
48
        (A = Args.getLastArg(options::OPT_client__name)) ||
303
48
        (A = Args.getLastArg(options::OPT_force__flat__namespace)) ||
304
48
        (A = Args.getLastArg(options::OPT_keep__private__externs)) ||
305
48
        (A = Args.getLastArg(options::OPT_private__bundle)))
306
0
      D.Diag(diag::err_drv_argument_not_allowed_with) << A->getAsString(Args)
307
0
                                                      << "-dynamiclib";
308
48
309
48
    Args.AddAllArgsTranslated(CmdArgs, options::OPT_compatibility__version,
310
48
                              "-dylib_compatibility_version");
311
48
    Args.AddAllArgsTranslated(CmdArgs, options::OPT_current__version,
312
48
                              "-dylib_current_version");
313
48
314
48
    AddMachOArch(Args, CmdArgs);
315
48
316
48
    Args.AddAllArgsTranslated(CmdArgs, options::OPT_install__name,
317
48
                              "-dylib_install_name");
318
48
  }
319
2.63k
320
2.63k
  Args.AddLastArg(CmdArgs, options::OPT_all__load);
321
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_allowable__client);
322
2.63k
  Args.AddLastArg(CmdArgs, options::OPT_bind__at__load);
323
2.63k
  if (MachOTC.isTargetIOSBased())
324
0
    Args.AddLastArg(CmdArgs, options::OPT_arch__errors__fatal);
325
2.63k
  Args.AddLastArg(CmdArgs, options::OPT_dead__strip);
326
2.63k
  Args.AddLastArg(CmdArgs, options::OPT_no__dead__strip__inits__and__terms);
327
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_dylib__file);
328
2.63k
  Args.AddLastArg(CmdArgs, options::OPT_dynamic);
329
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_exported__symbols__list);
330
2.63k
  Args.AddLastArg(CmdArgs, options::OPT_flat__namespace);
331
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_force__load);
332
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_headerpad__max__install__names);
333
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_image__base);
334
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_init);
335
2.63k
336
2.63k
  // Add the deployment target.
337
2.63k
  MachOTC.addMinVersionArgs(Args, CmdArgs);
338
2.63k
339
2.63k
  Args.AddLastArg(CmdArgs, options::OPT_nomultidefs);
340
2.63k
  Args.AddLastArg(CmdArgs, options::OPT_multi__module);
341
2.63k
  Args.AddLastArg(CmdArgs, options::OPT_single__module);
342
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined);
343
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_multiply__defined__unused);
344
2.63k
345
2.63k
  if (const Arg *A =
346
2
          Args.getLastArg(options::OPT_fpie, options::OPT_fPIE,
347
2
                          options::OPT_fno_pie, options::OPT_fno_PIE)) {
348
2
    if (A->getOption().matches(options::OPT_fpie) ||
349
2
        
A->getOption().matches(options::OPT_fPIE)1
)
350
1
      CmdArgs.push_back("-pie");
351
1
    else
352
1
      CmdArgs.push_back("-no_pie");
353
2
  }
354
2.63k
355
2.63k
  // for embed-bitcode, use -bitcode_bundle in linker command
356
2.63k
  if (C.getDriver().embedBitcodeEnabled()) {
357
7
    // Check if the toolchain supports bitcode build flow.
358
7
    if (MachOTC.SupportsEmbeddedBitcode()) {
359
6
      CmdArgs.push_back("-bitcode_bundle");
360
6
      if (C.getDriver().embedBitcodeMarkerOnly() && 
Version[0] >= 2783
) {
361
2
        CmdArgs.push_back("-bitcode_process_mode");
362
2
        CmdArgs.push_back("marker");
363
2
      }
364
6
    } else
365
1
      D.Diag(diag::err_drv_bitcode_unsupported_on_toolchain);
366
7
  }
367
2.63k
368
2.63k
  Args.AddLastArg(CmdArgs, options::OPT_prebind);
369
2.63k
  Args.AddLastArg(CmdArgs, options::OPT_noprebind);
370
2.63k
  Args.AddLastArg(CmdArgs, options::OPT_nofixprebinding);
371
2.63k
  Args.AddLastArg(CmdArgs, options::OPT_prebind__all__twolevel__modules);
372
2.63k
  Args.AddLastArg(CmdArgs, options::OPT_read__only__relocs);
373
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_sectcreate);
374
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_sectorder);
375
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_seg1addr);
376
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_segprot);
377
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_segaddr);
378
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_segs__read__only__addr);
379
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_segs__read__write__addr);
380
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table);
381
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_seg__addr__table__filename);
382
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_sub__library);
383
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_sub__umbrella);
384
2.63k
385
2.63k
  // Give --sysroot= preference, over the Apple specific behavior to also use
386
2.63k
  // --isysroot as the syslibroot.
387
2.63k
  StringRef sysroot = C.getSysRoot();
388
2.63k
  if (sysroot != "") {
389
8
    CmdArgs.push_back("-syslibroot");
390
8
    CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
391
2.62k
  } else if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
392
2.09k
    CmdArgs.push_back("-syslibroot");
393
2.09k
    CmdArgs.push_back(A->getValue());
394
2.09k
  }
395
2.63k
396
2.63k
  Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace);
397
2.63k
  Args.AddLastArg(CmdArgs, options::OPT_twolevel__namespace__hints);
398
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_umbrella);
399
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_undefined);
400
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_unexported__symbols__list);
401
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_weak__reference__mismatches);
402
2.63k
  Args.AddLastArg(CmdArgs, options::OPT_X_Flag);
403
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_y);
404
2.63k
  Args.AddLastArg(CmdArgs, options::OPT_w);
405
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_pagezero__size);
406
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_segs__read__);
407
2.63k
  Args.AddLastArg(CmdArgs, options::OPT_seglinkedit);
408
2.63k
  Args.AddLastArg(CmdArgs, options::OPT_noseglinkedit);
409
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_sectalign);
410
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_sectobjectsymbols);
411
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_segcreate);
412
2.63k
  Args.AddLastArg(CmdArgs, options::OPT_whyload);
413
2.63k
  Args.AddLastArg(CmdArgs, options::OPT_whatsloaded);
414
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_dylinker__install__name);
415
2.63k
  Args.AddLastArg(CmdArgs, options::OPT_dylinker);
416
2.63k
  Args.AddLastArg(CmdArgs, options::OPT_Mach);
417
2.63k
}
418
419
/// Determine whether we are linking the ObjC runtime.
420
2.63k
static bool isObjCRuntimeLinked(const ArgList &Args) {
421
2.63k
  if (isObjCAutoRefCount(Args)) {
422
1
    Args.ClaimAllArgs(options::OPT_fobjc_link_runtime);
423
1
    return true;
424
1
  }
425
2.62k
  return Args.hasArg(options::OPT_fobjc_link_runtime);
426
2.62k
}
427
428
void darwin::Linker::ConstructJob(Compilation &C, const JobAction &JA,
429
                                  const InputInfo &Output,
430
                                  const InputInfoList &Inputs,
431
                                  const ArgList &Args,
432
2.63k
                                  const char *LinkingOutput) const {
433
2.63k
  assert(Output.getType() == types::TY_Image && "Invalid linker output type.");
434
2.63k
435
2.63k
  // If the number of arguments surpasses the system limits, we will encode the
436
2.63k
  // input files in a separate file, shortening the command line. To this end,
437
2.63k
  // build a list of input file names that can be passed via a file with the
438
2.63k
  // -filelist linker option.
439
2.63k
  llvm::opt::ArgStringList InputFileList;
440
2.63k
441
2.63k
  // The logic here is derived from gcc's behavior; most of which
442
2.63k
  // comes from specs (starting with link_command). Consult gcc for
443
2.63k
  // more information.
444
2.63k
  ArgStringList CmdArgs;
445
2.63k
446
2.63k
  /// Hack(tm) to ignore linking errors when we are doing ARC migration.
447
2.63k
  if (Args.hasArg(options::OPT_ccc_arcmt_check,
448
2.63k
                  options::OPT_ccc_arcmt_migrate)) {
449
2
    for (const auto &Arg : Args)
450
18
      Arg->claim();
451
2
    const char *Exec =
452
2
        Args.MakeArgString(getToolChain().GetProgramPath("touch"));
453
2
    CmdArgs.push_back(Output.getFilename());
454
2
    C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, None));
455
2
    return;
456
2
  }
457
2.63k
458
2.63k
  // I'm not sure why this particular decomposition exists in gcc, but
459
2.63k
  // we follow suite for ease of comparison.
460
2.63k
  AddLinkArgs(C, Args, CmdArgs, Inputs);
461
2.63k
462
2.63k
  // For LTO, pass the name of the optimization record file and other
463
2.63k
  // opt-remarks flags.
464
2.63k
  if (Args.hasFlag(options::OPT_fsave_optimization_record,
465
2.63k
                   options::OPT_fsave_optimization_record_EQ,
466
2.63k
                   options::OPT_fno_save_optimization_record, false)) {
467
8
    CmdArgs.push_back("-mllvm");
468
8
    CmdArgs.push_back("-lto-pass-remarks-output");
469
8
    CmdArgs.push_back("-mllvm");
470
8
471
8
    SmallString<128> F;
472
8
    F = Output.getFilename();
473
8
    F += ".opt.";
474
8
    if (const Arg *A =
475
1
            Args.getLastArg(options::OPT_fsave_optimization_record_EQ))
476
1
      F += A->getValue();
477
7
    else
478
7
      F += "yaml";
479
8
480
8
    CmdArgs.push_back(Args.MakeArgString(F));
481
8
482
8
    if (getLastProfileUseArg(Args)) {
483
2
      CmdArgs.push_back("-mllvm");
484
2
      CmdArgs.push_back("-lto-pass-remarks-with-hotness");
485
2
486
2
      if (const Arg *A =
487
1
              Args.getLastArg(options::OPT_fdiagnostics_hotness_threshold_EQ)) {
488
1
        CmdArgs.push_back("-mllvm");
489
1
        std::string Opt =
490
1
            std::string("-lto-pass-remarks-hotness-threshold=") + A->getValue();
491
1
        CmdArgs.push_back(Args.MakeArgString(Opt));
492
1
      }
493
2
    }
494
8
495
8
    if (const Arg *A =
496
1
            Args.getLastArg(options::OPT_foptimization_record_passes_EQ)) {
497
1
      CmdArgs.push_back("-mllvm");
498
1
      std::string Passes =
499
1
          std::string("-lto-pass-remarks-filter=") + A->getValue();
500
1
      CmdArgs.push_back(Args.MakeArgString(Passes));
501
1
    }
502
8
503
8
    if (const Arg *A =
504
1
            Args.getLastArg(options::OPT_fsave_optimization_record_EQ)) {
505
1
      CmdArgs.push_back("-mllvm");
506
1
      std::string Format =
507
1
          std::string("-lto-pass-remarks-format=") + A->getValue();
508
1
      CmdArgs.push_back(Args.MakeArgString(Format));
509
1
    }
510
8
  }
511
2.63k
512
2.63k
  // Propagate the -moutline flag to the linker in LTO.
513
2.63k
  if (Arg *A =
514
2
          Args.getLastArg(options::OPT_moutline, options::OPT_mno_outline)) {
515
2
    if (A->getOption().matches(options::OPT_moutline)) {
516
1
      if (getMachOToolChain().getMachOArchName(Args) == "arm64") {
517
1
        CmdArgs.push_back("-mllvm");
518
1
        CmdArgs.push_back("-enable-machine-outliner");
519
1
520
1
        // Outline from linkonceodr functions by default in LTO.
521
1
        CmdArgs.push_back("-mllvm");
522
1
        CmdArgs.push_back("-enable-linkonceodr-outlining");
523
1
      }
524
1
    } else {
525
1
      // Disable all outlining behaviour if we have mno-outline. We need to do
526
1
      // this explicitly, because targets which support default outlining will
527
1
      // try to do work if we don't.
528
1
      CmdArgs.push_back("-mllvm");
529
1
      CmdArgs.push_back("-enable-machine-outliner=never");
530
1
    }
531
2
  }
532
2.63k
533
2.63k
  // Setup statistics file output.
534
2.63k
  SmallString<128> StatsFile =
535
2.63k
      getStatsFileName(Args, Output, Inputs[0], getToolChain().getDriver());
536
2.63k
  if (!StatsFile.empty()) {
537
2
    CmdArgs.push_back("-mllvm");
538
2
    CmdArgs.push_back(Args.MakeArgString("-lto-stats-file=" + StatsFile.str()));
539
2
  }
540
2.63k
541
2.63k
  // It seems that the 'e' option is completely ignored for dynamic executables
542
2.63k
  // (the default), and with static executables, the last one wins, as expected.
543
2.63k
  Args.AddAllArgs(CmdArgs, {options::OPT_d_Flag, options::OPT_s, options::OPT_t,
544
2.63k
                            options::OPT_Z_Flag, options::OPT_u_Group,
545
2.63k
                            options::OPT_e, options::OPT_r});
546
2.63k
547
2.63k
  // Forward -ObjC when either -ObjC or -ObjC++ is used, to force loading
548
2.63k
  // members of static archive libraries which implement Objective-C classes or
549
2.63k
  // categories.
550
2.63k
  if (Args.hasArg(options::OPT_ObjC) || Args.hasArg(options::OPT_ObjCXX))
551
0
    CmdArgs.push_back("-ObjC");
552
2.63k
553
2.63k
  CmdArgs.push_back("-o");
554
2.63k
  CmdArgs.push_back(Output.getFilename());
555
2.63k
556
2.63k
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles))
557
2.62k
    getMachOToolChain().addStartObjectFileArgs(Args, CmdArgs);
558
2.63k
559
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_L);
560
2.63k
561
2.63k
  AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
562
2.63k
  // Build the input file for -filelist (list of linker input files) in case we
563
2.63k
  // need it later
564
16.9k
  for (const auto &II : Inputs) {
565
16.9k
    if (!II.isFilename()) {
566
5.34k
      // This is a linker input argument.
567
5.34k
      // We cannot mix input arguments and file names in a -filelist input, thus
568
5.34k
      // we prematurely stop our list (remaining files shall be passed as
569
5.34k
      // arguments).
570
5.34k
      if (InputFileList.size() > 0)
571
545
        break;
572
4.79k
573
4.79k
      continue;
574
4.79k
    }
575
11.6k
576
11.6k
    InputFileList.push_back(II.getFilename());
577
11.6k
  }
578
2.63k
579
2.63k
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs))
580
2.54k
    addOpenMPRuntime(CmdArgs, getToolChain(), Args);
581
2.63k
582
2.63k
  if (isObjCRuntimeLinked(Args) &&
583
2.63k
      
!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)6
) {
584
5
    // We use arclite library for both ARC and subscripting support.
585
5
    getMachOToolChain().AddLinkARCArgs(Args, CmdArgs);
586
5
587
5
    CmdArgs.push_back("-framework");
588
5
    CmdArgs.push_back("Foundation");
589
5
    // Link libobj.
590
5
    CmdArgs.push_back("-lobjc");
591
5
  }
592
2.63k
593
2.63k
  if (LinkingOutput) {
594
48
    CmdArgs.push_back("-arch_multiple");
595
48
    CmdArgs.push_back("-final_output");
596
48
    CmdArgs.push_back(LinkingOutput);
597
48
  }
598
2.63k
599
2.63k
  if (Args.hasArg(options::OPT_fnested_functions))
600
0
    CmdArgs.push_back("-allow_stack_execute");
601
2.63k
602
2.63k
  getMachOToolChain().addProfileRTLibs(Args, CmdArgs);
603
2.63k
604
2.63k
  if (unsigned Parallelism =
605
1
          getLTOParallelism(Args, getToolChain().getDriver())) {
606
1
    CmdArgs.push_back("-mllvm");
607
1
    CmdArgs.push_back(Args.MakeArgString("-threads=" + Twine(Parallelism)));
608
1
  }
609
2.63k
610
2.63k
  if (getToolChain().ShouldLinkCXXStdlib(Args))
611
1.58k
    getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
612
2.63k
613
2.63k
  bool NoStdOrDefaultLibs =
614
2.63k
      Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs);
615
2.63k
  bool ForceLinkBuiltins = Args.hasArg(options::OPT_fapple_link_rtlib);
616
2.63k
  if (!NoStdOrDefaultLibs || 
ForceLinkBuiltins85
) {
617
2.54k
    // link_ssp spec is empty.
618
2.54k
619
2.54k
    // If we have both -nostdlib/nodefaultlibs and -fapple-link-rtlib then
620
2.54k
    // we just want to link the builtins, not the other libs like libSystem.
621
2.54k
    if (NoStdOrDefaultLibs && 
ForceLinkBuiltins1
) {
622
1
      getMachOToolChain().AddLinkRuntimeLib(Args, CmdArgs, "builtins");
623
2.54k
    } else {
624
2.54k
      // Let the tool chain choose which runtime library to link.
625
2.54k
      getMachOToolChain().AddLinkRuntimeLibArgs(Args, CmdArgs,
626
2.54k
                                                ForceLinkBuiltins);
627
2.54k
628
2.54k
      // No need to do anything for pthreads. Claim argument to avoid warning.
629
2.54k
      Args.ClaimAllArgs(options::OPT_pthread);
630
2.54k
      Args.ClaimAllArgs(options::OPT_pthreads);
631
2.54k
    }
632
2.54k
  }
633
2.63k
634
2.63k
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
635
2.62k
    // endfile_spec is empty.
636
2.62k
  }
637
2.63k
638
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
639
2.63k
  Args.AddAllArgs(CmdArgs, options::OPT_F);
640
2.63k
641
2.63k
  // -iframework should be forwarded as -F.
642
2.63k
  for (const Arg *A : Args.filtered(options::OPT_iframework))
643
1
    CmdArgs.push_back(Args.MakeArgString(std::string("-F") + A->getValue()));
644
2.63k
645
2.63k
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
646
2.54k
    if (Arg *A = Args.getLastArg(options::OPT_fveclib)) {
647
1
      if (A->getValue() == StringRef("Accelerate")) {
648
1
        CmdArgs.push_back("-framework");
649
1
        CmdArgs.push_back("Accelerate");
650
1
      }
651
1
    }
652
2.54k
  }
653
2.63k
654
2.63k
  const char *Exec = Args.MakeArgString(getToolChain().GetLinkerPath());
655
2.63k
  std::unique_ptr<Command> Cmd =
656
2.63k
      llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs);
657
2.63k
  Cmd->setInputFileList(std::move(InputFileList));
658
2.63k
  C.addCommand(std::move(Cmd));
659
2.63k
}
660
661
void darwin::Lipo::ConstructJob(Compilation &C, const JobAction &JA,
662
                                const InputInfo &Output,
663
                                const InputInfoList &Inputs,
664
                                const ArgList &Args,
665
856
                                const char *LinkingOutput) const {
666
856
  ArgStringList CmdArgs;
667
856
668
856
  CmdArgs.push_back("-create");
669
856
  assert(Output.isFilename() && "Unexpected lipo output.");
670
856
671
856
  CmdArgs.push_back("-output");
672
856
  CmdArgs.push_back(Output.getFilename());
673
856
674
2.19k
  for (const auto &II : Inputs) {
675
2.19k
    assert(II.isFilename() && "Unexpected lipo input.");
676
2.19k
    CmdArgs.push_back(II.getFilename());
677
2.19k
  }
678
856
679
856
  const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath("lipo"));
680
856
  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
681
856
}
682
683
void darwin::Dsymutil::ConstructJob(Compilation &C, const JobAction &JA,
684
                                    const InputInfo &Output,
685
                                    const InputInfoList &Inputs,
686
                                    const ArgList &Args,
687
30
                                    const char *LinkingOutput) const {
688
30
  ArgStringList CmdArgs;
689
30
690
30
  CmdArgs.push_back("-o");
691
30
  CmdArgs.push_back(Output.getFilename());
692
30
693
30
  assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
694
30
  const InputInfo &Input = Inputs[0];
695
30
  assert(Input.isFilename() && "Unexpected dsymutil input.");
696
30
  CmdArgs.push_back(Input.getFilename());
697
30
698
30
  const char *Exec =
699
30
      Args.MakeArgString(getToolChain().GetProgramPath("dsymutil"));
700
30
  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
701
30
}
702
703
void darwin::VerifyDebug::ConstructJob(Compilation &C, const JobAction &JA,
704
                                       const InputInfo &Output,
705
                                       const InputInfoList &Inputs,
706
                                       const ArgList &Args,
707
0
                                       const char *LinkingOutput) const {
708
0
  ArgStringList CmdArgs;
709
0
  CmdArgs.push_back("--verify");
710
0
  CmdArgs.push_back("--debug-info");
711
0
  CmdArgs.push_back("--eh-frame");
712
0
  CmdArgs.push_back("--quiet");
713
0
714
0
  assert(Inputs.size() == 1 && "Unable to handle multiple inputs.");
715
0
  const InputInfo &Input = Inputs[0];
716
0
  assert(Input.isFilename() && "Unexpected verify input");
717
0
718
0
  // Grabbing the output of the earlier dsymutil run.
719
0
  CmdArgs.push_back(Input.getFilename());
720
0
721
0
  const char *Exec =
722
0
      Args.MakeArgString(getToolChain().GetProgramPath("dwarfdump"));
723
0
  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
724
0
}
725
726
MachO::MachO(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
727
22.6k
    : ToolChain(D, Triple, Args) {
728
22.6k
  // We expect 'as', 'ld', etc. to be adjacent to our install dir.
729
22.6k
  getProgramPaths().push_back(getDriver().getInstalledDir());
730
22.6k
  if (getDriver().getInstalledDir() != getDriver().Dir)
731
5.72k
    getProgramPaths().push_back(getDriver().Dir);
732
22.6k
}
733
734
/// Darwin - Darwin tool chain for i386 and x86_64.
735
Darwin::Darwin(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
736
    : MachO(D, Triple, Args), TargetInitialized(false),
737
21.8k
      CudaInstallation(D, Triple, Args) {}
738
739
28.6k
types::ID MachO::LookupTypeForExtension(StringRef Ext) const {
740
28.6k
  types::ID Ty = types::lookupTypeForExtension(Ext);
741
28.6k
742
28.6k
  // Darwin always preprocesses assembly files (unless -x is used explicitly).
743
28.6k
  if (Ty == types::TY_PP_Asm)
744
23
    return types::TY_Asm;
745
28.6k
746
28.6k
  return Ty;
747
28.6k
}
748
749
17.0k
bool MachO::HasNativeLLVMSupport() const { return true; }
750
751
22.0k
ToolChain::CXXStdlibType Darwin::GetDefaultCXXStdlibType() const {
752
22.0k
  // Default to use libc++ on OS X 10.9+ and iOS 7+.
753
22.0k
  if ((isTargetMacOS() && 
!isMacosxVersionLT(10, 9)10.2k
) ||
754
22.0k
       
(13.5k
isTargetIOSBased()13.5k
&&
!isIPhoneOSVersionLT(7, 0)11.7k
) ||
755
22.0k
       
isTargetWatchOSBased()4.85k
)
756
17.2k
    return ToolChain::CST_Libcxx;
757
4.80k
758
4.80k
  return ToolChain::CST_Libstdcxx;
759
4.80k
}
760
761
/// Darwin provides an ARC runtime starting in MacOS X 10.7 and iOS 5.0.
762
20.0k
ObjCRuntime Darwin::getDefaultObjCRuntime(bool isNonFragile) const {
763
20.0k
  if (isTargetWatchOSBased())
764
38
    return ObjCRuntime(ObjCRuntime::WatchOS, TargetVersion);
765
19.9k
  if (isTargetIOSBased())
766
9.90k
    return ObjCRuntime(ObjCRuntime::iOS, TargetVersion);
767
10.0k
  if (isNonFragile)
768
9.82k
    return ObjCRuntime(ObjCRuntime::MacOSX, TargetVersion);
769
258
  return ObjCRuntime(ObjCRuntime::FragileMacOSX, TargetVersion);
770
258
}
771
772
/// Darwin provides a blocks runtime starting in MacOS X 10.6 and iOS 3.2.
773
20.0k
bool Darwin::hasBlocksRuntime() const {
774
20.0k
  if (isTargetWatchOSBased())
775
38
    return true;
776
20.0k
  else if (isTargetIOSBased())
777
9.90k
    return !isIPhoneOSVersionLT(3, 2);
778
10.1k
  else {
779
10.1k
    assert(isTargetMacOS() && "unexpected darwin target");
780
10.1k
    return !isMacosxVersionLT(10, 6);
781
10.1k
  }
782
20.0k
}
783
784
void Darwin::AddCudaIncludeArgs(const ArgList &DriverArgs,
785
28
                                ArgStringList &CC1Args) const {
786
28
  CudaInstallation.AddCudaIncludeArgs(DriverArgs, CC1Args);
787
28
}
788
789
// This is just a MachO name translation routine and there's no
790
// way to join this into ARMTargetParser without breaking all
791
// other assumptions. Maybe MachO should consider standardising
792
// their nomenclature.
793
111
static const char *ArmMachOArchName(StringRef Arch) {
794
111
  return llvm::StringSwitch<const char *>(Arch)
795
111
      .Case("armv6k", "armv6")
796
111
      .Case("armv6m", "armv6m")
797
111
      .Case("armv5tej", "armv5")
798
111
      .Case("xscale", "xscale")
799
111
      .Case("armv4t", "armv4t")
800
111
      .Case("armv7", "armv7")
801
111
      .Cases("armv7a", "armv7-a", "armv7")
802
111
      .Cases("armv7r", "armv7-r", "armv7")
803
111
      .Cases("armv7em", "armv7e-m", "armv7em")
804
111
      .Cases("armv7k", "armv7-k", "armv7k")
805
111
      .Cases("armv7m", "armv7-m", "armv7m")
806
111
      .Cases("armv7s", "armv7-s", "armv7s")
807
111
      .Default(nullptr);
808
111
}
809
810
0
static const char *ArmMachOArchNameCPU(StringRef CPU) {
811
0
  llvm::ARM::ArchKind ArchKind = llvm::ARM::parseCPUArch(CPU);
812
0
  if (ArchKind == llvm::ARM::ArchKind::INVALID)
813
0
    return nullptr;
814
0
  StringRef Arch = llvm::ARM::getArchName(ArchKind);
815
0
816
0
  // FIXME: Make sure this MachO triple mangling is really necessary.
817
0
  // ARMv5* normalises to ARMv5.
818
0
  if (Arch.startswith("armv5"))
819
0
    Arch = Arch.substr(0, 5);
820
0
  // ARMv6*, except ARMv6M, normalises to ARMv6.
821
0
  else if (Arch.startswith("armv6") && !Arch.endswith("6m"))
822
0
    Arch = Arch.substr(0, 5);
823
0
  // ARMv7A normalises to ARMv7.
824
0
  else if (Arch.endswith("v7a"))
825
0
    Arch = Arch.substr(0, 5);
826
0
  return Arch.data();
827
0
}
828
829
8.78k
StringRef MachO::getMachOArchName(const ArgList &Args) const {
830
8.78k
  switch (getTriple().getArch()) {
831
8.78k
  default:
832
6.68k
    return getDefaultUniversalArchName();
833
8.78k
834
8.78k
  case llvm::Triple::aarch64:
835
1.99k
    return "arm64";
836
8.78k
837
8.78k
  case llvm::Triple::thumb:
838
111
  case llvm::Triple::arm:
839
111
    if (const Arg *A = Args.getLastArg(clang::driver::options::OPT_march_EQ))
840
111
      if (const char *Arch = ArmMachOArchName(A->getValue()))
841
111
        return Arch;
842
0
843
0
    if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ))
844
0
      if (const char *Arch = ArmMachOArchNameCPU(A->getValue()))
845
0
        return Arch;
846
0
847
0
    return "arm";
848
8.78k
  }
849
8.78k
}
850
851
21.8k
Darwin::~Darwin() {}
852
853
22.6k
MachO::~MachO() {}
854
855
std::string Darwin::ComputeEffectiveClangTriple(const ArgList &Args,
856
24.2k
                                                types::ID InputType) const {
857
24.2k
  llvm::Triple Triple(ComputeLLVMTriple(Args, InputType));
858
24.2k
859
24.2k
  // If the target isn't initialized (e.g., an unknown Darwin platform, return
860
24.2k
  // the default triple).
861
24.2k
  if (!isTargetInitialized())
862
1
    return Triple.getTriple();
863
24.2k
864
24.2k
  SmallString<16> Str;
865
24.2k
  if (isTargetWatchOSBased())
866
53
    Str += "watchos";
867
24.1k
  else if (isTargetTvOSBased())
868
46
    Str += "tvos";
869
24.1k
  else if (isTargetIOSBased())
870
12.8k
    Str += "ios";
871
11.3k
  else
872
11.3k
    Str += "macosx";
873
24.2k
  Str += getTargetVersion().getAsString();
874
24.2k
  Triple.setOSName(Str);
875
24.2k
876
24.2k
  return Triple.getTriple();
877
24.2k
}
878
879
3.57k
Tool *MachO::getTool(Action::ActionClass AC) const {
880
3.57k
  switch (AC) {
881
3.57k
  case Action::LipoJobClass:
882
858
    if (!Lipo)
883
858
      Lipo.reset(new tools::darwin::Lipo(*this));
884
858
    return Lipo.get();
885
3.57k
  case Action::DsymutilJobClass:
886
35
    if (!Dsymutil)
887
35
      Dsymutil.reset(new tools::darwin::Dsymutil(*this));
888
35
    return Dsymutil.get();
889
3.57k
  case Action::VerifyDebugInfoJobClass:
890
2
    if (!VerifyDebug)
891
2
      VerifyDebug.reset(new tools::darwin::VerifyDebug(*this));
892
2
    return VerifyDebug.get();
893
3.57k
  default:
894
2.67k
    return ToolChain::getTool(AC);
895
3.57k
  }
896
3.57k
}
897
898
2.63k
Tool *MachO::buildLinker() const { return new tools::darwin::Linker(*this); }
899
900
14
Tool *MachO::buildAssembler() const {
901
14
  return new tools::darwin::Assembler(*this);
902
14
}
903
904
DarwinClang::DarwinClang(const Driver &D, const llvm::Triple &Triple,
905
                         const ArgList &Args)
906
21.8k
    : Darwin(D, Triple, Args) {}
907
908
20.1k
void DarwinClang::addClangWarningOptions(ArgStringList &CC1Args) const {
909
20.1k
  // For modern targets, promote certain warnings to errors.
910
20.1k
  if (isTargetWatchOSBased() || 
getTriple().isArch64Bit()20.1k
) {
911
17.3k
    // Always enable -Wdeprecated-objc-isa-usage and promote it
912
17.3k
    // to an error.
913
17.3k
    CC1Args.push_back("-Wdeprecated-objc-isa-usage");
914
17.3k
    CC1Args.push_back("-Werror=deprecated-objc-isa-usage");
915
17.3k
916
17.3k
    // For iOS and watchOS, also error about implicit function declarations,
917
17.3k
    // as that can impact calling conventions.
918
17.3k
    if (!isTargetMacOS())
919
7.92k
      CC1Args.push_back("-Werror=implicit-function-declaration");
920
17.3k
  }
921
20.1k
}
922
923
/// Take a path that speculatively points into Xcode and return the
924
/// `XCODE/Contents/Developer` path if it is an Xcode path, or an empty path
925
/// otherwise.
926
4
static StringRef getXcodeDeveloperPath(StringRef PathIntoXcode) {
927
4
  static constexpr llvm::StringLiteral XcodeAppSuffix(
928
4
      ".app/Contents/Developer");
929
4
  size_t Index = PathIntoXcode.find(XcodeAppSuffix);
930
4
  if (Index == StringRef::npos)
931
3
    return "";
932
1
  return PathIntoXcode.take_front(Index + XcodeAppSuffix.size());
933
1
}
934
935
void DarwinClang::AddLinkARCArgs(const ArgList &Args,
936
5
                                 ArgStringList &CmdArgs) const {
937
5
  // Avoid linking compatibility stubs on i386 mac.
938
5
  if (isTargetMacOS() && getArch() == llvm::Triple::x86)
939
1
    return;
940
4
941
4
  ObjCRuntime runtime = getDefaultObjCRuntime(/*nonfragile*/ true);
942
4
943
4
  if ((runtime.hasNativeARC() || 
!isObjCAutoRefCount(Args)0
) &&
944
4
      runtime.hasSubscripting())
945
1
    return;
946
3
947
3
  SmallString<128> P(getDriver().ClangExecutable);
948
3
  llvm::sys::path::remove_filename(P); // 'clang'
949
3
  llvm::sys::path::remove_filename(P); // 'bin'
950
3
951
3
  // 'libarclite' usually lives in the same toolchain as 'clang'. However, the
952
3
  // Swift open source toolchains for macOS distribute Clang without libarclite.
953
3
  // In that case, to allow the linker to find 'libarclite', we point to the
954
3
  // 'libarclite' in the XcodeDefault toolchain instead.
955
3
  if (getXcodeDeveloperPath(P).empty()) {
956
3
    if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
957
1
      // Try to infer the path to 'libarclite' in the toolchain from the
958
1
      // specified SDK path.
959
1
      StringRef XcodePathForSDK = getXcodeDeveloperPath(A->getValue());
960
1
      if (!XcodePathForSDK.empty()) {
961
1
        P = XcodePathForSDK;
962
1
        llvm::sys::path::append(P, "Toolchains/XcodeDefault.xctoolchain/usr");
963
1
      }
964
1
    }
965
3
  }
966
3
967
3
  CmdArgs.push_back("-force_load");
968
3
  llvm::sys::path::append(P, "lib", "arc", "libarclite_");
969
3
  // Mash in the platform.
970
3
  if (isTargetWatchOSSimulator())
971
0
    P += "watchsimulator";
972
3
  else if (isTargetWatchOS())
973
0
    P += "watchos";
974
3
  else if (isTargetTvOSSimulator())
975
0
    P += "appletvsimulator";
976
3
  else if (isTargetTvOS())
977
0
    P += "appletvos";
978
3
  else if (isTargetIOSSimulator())
979
0
    P += "iphonesimulator";
980
3
  else if (isTargetIPhoneOS())
981
0
    P += "iphoneos";
982
3
  else
983
3
    P += "macosx";
984
3
  P += ".a";
985
3
986
3
  CmdArgs.push_back(Args.MakeArgString(P));
987
3
}
988
989
2.98k
unsigned DarwinClang::GetDefaultDwarfVersion() const {
990
2.98k
  // Default to use DWARF 2 on OS X 10.10 / iOS 8 and lower.
991
2.98k
  if ((isTargetMacOS() && 
isMacosxVersionLT(10, 11)1.03k
) ||
992
2.98k
      
(2.27k
isTargetIOSBased()2.27k
&&
isIPhoneOSVersionLT(9)1.94k
))
993
2.64k
    return 2;
994
337
  return 4;
995
337
}
996
997
void MachO::AddLinkRuntimeLib(const ArgList &Args, ArgStringList &CmdArgs,
998
                              StringRef Component, RuntimeLinkOptions Opts,
999
2.63k
                              bool IsShared) const {
1000
2.63k
  SmallString<64> DarwinLibName = StringRef("libclang_rt.");
1001
2.63k
  // an Darwin the builtins compomnent is not in the library name
1002
2.63k
  if (Component != "builtins") {
1003
100
    DarwinLibName += Component;
1004
100
    if (!(Opts & RLO_IsEmbedded))
1005
87
      DarwinLibName += "_";
1006
100
    DarwinLibName += getOSLibraryNameSuffix();
1007
100
  } else
1008
2.53k
    DarwinLibName += getOSLibraryNameSuffix(true);
1009
2.63k
1010
2.63k
  DarwinLibName += IsShared ? 
"_dynamic.dylib"56
:
".a"2.57k
;
1011
2.63k
  SmallString<128> Dir(getDriver().ResourceDir);
1012
2.63k
  llvm::sys::path::append(
1013
2.63k
      Dir, "lib", (Opts & RLO_IsEmbedded) ? 
"macho_embedded"13
:
"darwin"2.61k
);
1014
2.63k
1015
2.63k
  SmallString<128> P(Dir);
1016
2.63k
  llvm::sys::path::append(P, DarwinLibName);
1017
2.63k
1018
2.63k
  // For now, allow missing resource libraries to support developers who may
1019
2.63k
  // not have compiler-rt checked out or integrated into their build (unless
1020
2.63k
  // we explicitly force linking with this library).
1021
2.63k
  if ((Opts & RLO_AlwaysLink) || 
getVFS().exists(P)2.54k
) {
1022
2.57k
    const char *LibArg = Args.MakeArgString(P);
1023
2.57k
    if (Opts & RLO_FirstLink)
1024
26
      CmdArgs.insert(CmdArgs.begin(), LibArg);
1025
2.55k
    else
1026
2.55k
      CmdArgs.push_back(LibArg);
1027
2.57k
  }
1028
2.63k
1029
2.63k
  // Adding the rpaths might negatively interact when other rpaths are involved,
1030
2.63k
  // so we should make sure we add the rpaths last, after all user-specified
1031
2.63k
  // rpaths. This is currently true from this place, but we need to be
1032
2.63k
  // careful if this function is ever called before user's rpaths are emitted.
1033
2.63k
  if (Opts & RLO_AddRPath) {
1034
56
    assert(DarwinLibName.endswith(".dylib") && "must be a dynamic library");
1035
56
1036
56
    // Add @executable_path to rpath to support having the dylib copied with
1037
56
    // the executable.
1038
56
    CmdArgs.push_back("-rpath");
1039
56
    CmdArgs.push_back("@executable_path");
1040
56
1041
56
    // Add the path to the resource dir to rpath to support using the dylib
1042
56
    // from the default location without copying.
1043
56
    CmdArgs.push_back("-rpath");
1044
56
    CmdArgs.push_back(Args.MakeArgString(Dir));
1045
56
  }
1046
2.63k
}
1047
1048
15.6k
StringRef Darwin::getPlatformFamily() const {
1049
15.6k
  switch (TargetPlatform) {
1050
15.6k
    case DarwinPlatformKind::MacOS:
1051
3.88k
      return "MacOSX";
1052
15.6k
    case DarwinPlatformKind::IPhoneOS:
1053
11.7k
      return "iPhone";
1054
15.6k
    case DarwinPlatformKind::TvOS:
1055
7
      return "AppleTV";
1056
15.6k
    case DarwinPlatformKind::WatchOS:
1057
8
      return "Watch";
1058
0
  }
1059
0
  llvm_unreachable("Unsupported platform");
1060
0
}
1061
1062
26.8k
StringRef Darwin::getSDKName(StringRef isysroot) {
1063
26.8k
  // Assume SDK has path: SOME_PATH/SDKs/PlatformXX.YY.sdk
1064
26.8k
  llvm::sys::path::const_iterator SDKDir;
1065
26.8k
  auto BeginSDK = llvm::sys::path::begin(isysroot);
1066
26.8k
  auto EndSDK = llvm::sys::path::end(isysroot);
1067
268k
  for (auto IT = BeginSDK; IT != EndSDK; 
++IT241k
) {
1068
268k
    StringRef SDK = *IT;
1069
268k
    if (SDK.endswith(".sdk"))
1070
26.7k
      return SDK.slice(0, SDK.size() - 4);
1071
268k
  }
1072
26.8k
  
return ""97
;
1073
26.8k
}
1074
1075
2.61k
StringRef Darwin::getOSLibraryNameSuffix(bool IgnoreSim) const {
1076
2.61k
  switch (TargetPlatform) {
1077
2.61k
  case DarwinPlatformKind::MacOS:
1078
522
    return "osx";
1079
2.61k
  case DarwinPlatformKind::IPhoneOS:
1080
2.05k
    return TargetEnvironment == NativeEnvironment || 
IgnoreSim26
?
"ios"2.05k
1081
2.05k
                                                               : 
"iossim"6
;
1082
2.61k
  case DarwinPlatformKind::TvOS:
1083
21
    return TargetEnvironment == NativeEnvironment || 
IgnoreSim11
?
"tvos"17
1084
21
                                                               : 
"tvossim"4
;
1085
2.61k
  case DarwinPlatformKind::WatchOS:
1086
19
    return TargetEnvironment == NativeEnvironment || 
IgnoreSim4
?
"watchos"18
1087
19
                                                               : 
"watchossim"1
;
1088
0
  }
1089
0
  llvm_unreachable("Unsupported platform");
1090
0
}
1091
1092
/// Check if the link command contains a symbol export directive.
1093
26
static bool hasExportSymbolDirective(const ArgList &Args) {
1094
182
  for (Arg *A : Args) {
1095
182
    if (A->getOption().matches(options::OPT_exported__symbols__list))
1096
2
      return true;
1097
180
    if (!A->getOption().matches(options::OPT_Wl_COMMA) &&
1098
180
        
!A->getOption().matches(options::OPT_Xlinker)176
)
1099
172
      continue;
1100
8
    if (A->containsValue("-exported_symbols_list") ||
1101
8
        
A->containsValue("-exported_symbol")4
)
1102
8
      return true;
1103
8
  }
1104
26
  
return false16
;
1105
26
}
1106
1107
/// Add an export directive for \p Symbol to the link command.
1108
40
static void addExportedSymbol(ArgStringList &CmdArgs, const char *Symbol) {
1109
40
  CmdArgs.push_back("-exported_symbol");
1110
40
  CmdArgs.push_back(Symbol);
1111
40
}
1112
1113
void Darwin::addProfileRTLibs(const ArgList &Args,
1114
2.61k
                              ArgStringList &CmdArgs) const {
1115
2.61k
  if (!needsProfileRT(Args)) 
return2.59k
;
1116
26
1117
26
  AddLinkRuntimeLib(Args, CmdArgs, "profile",
1118
26
                    RuntimeLinkOptions(RLO_AlwaysLink | RLO_FirstLink));
1119
26
1120
26
  // If we have a symbol export directive and we're linking in the profile
1121
26
  // runtime, automatically export symbols necessary to implement some of the
1122
26
  // runtime's functionality.
1123
26
  if (hasExportSymbolDirective(Args)) {
1124
10
    if (needsGCovInstrumentation(Args)) {
1125
5
      addExportedSymbol(CmdArgs, "___gcov_flush");
1126
5
      addExportedSymbol(CmdArgs, "_flush_fn_list");
1127
5
      addExportedSymbol(CmdArgs, "_writeout_fn_list");
1128
5
    } else {
1129
5
      addExportedSymbol(CmdArgs, "___llvm_profile_filename");
1130
5
      addExportedSymbol(CmdArgs, "___llvm_profile_raw_version");
1131
5
      addExportedSymbol(CmdArgs, "_lprofCurFilename");
1132
5
    }
1133
10
    addExportedSymbol(CmdArgs, "_lprofDirMode");
1134
10
  }
1135
26
}
1136
1137
void DarwinClang::AddLinkSanitizerLibArgs(const ArgList &Args,
1138
                                          ArgStringList &CmdArgs,
1139
                                          StringRef Sanitizer,
1140
60
                                          bool Shared) const {
1141
60
  auto RLO = RuntimeLinkOptions(RLO_AlwaysLink | (Shared ? 
RLO_AddRPath56
:
0U4
));
1142
60
  AddLinkRuntimeLib(Args, CmdArgs, Sanitizer, RLO, Shared);
1143
60
}
1144
1145
ToolChain::RuntimeLibType DarwinClang::GetRuntimeLibType(
1146
2.53k
    const ArgList &Args) const {
1147
2.53k
  if (Arg* A = Args.getLastArg(options::OPT_rtlib_EQ)) {
1148
0
    StringRef Value = A->getValue();
1149
0
    if (Value != "compiler-rt")
1150
0
      getDriver().Diag(clang::diag::err_drv_unsupported_rtlib_for_platform)
1151
0
          << Value << "darwin";
1152
0
  }
1153
2.53k
1154
2.53k
  return ToolChain::RLT_CompilerRT;
1155
2.53k
}
1156
1157
void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
1158
                                        ArgStringList &CmdArgs,
1159
2.53k
                                        bool ForceLinkBuiltinRT) const {
1160
2.53k
  // Call once to ensure diagnostic is printed if wrong value was specified
1161
2.53k
  GetRuntimeLibType(Args);
1162
2.53k
1163
2.53k
  // Darwin doesn't support real static executables, don't link any runtime
1164
2.53k
  // libraries with -static.
1165
2.53k
  if (Args.hasArg(options::OPT_static) ||
1166
2.53k
      
Args.hasArg(options::OPT_fapple_kext)2.52k
||
1167
2.53k
      
Args.hasArg(options::OPT_mkernel)2.52k
) {
1168
3
    if (ForceLinkBuiltinRT)
1169
1
      AddLinkRuntimeLib(Args, CmdArgs, "builtins");
1170
3
    return;
1171
3
  }
1172
2.52k
1173
2.52k
  // Reject -static-libgcc for now, we can deal with this when and if someone
1174
2.52k
  // cares. This is useful in situations where someone wants to statically link
1175
2.52k
  // something like libstdc++, and needs its runtime support routines.
1176
2.52k
  if (const Arg *A = Args.getLastArg(options::OPT_static_libgcc)) {
1177
0
    getDriver().Diag(diag::err_drv_unsupported_opt) << A->getAsString(Args);
1178
0
    return;
1179
0
  }
1180
2.52k
1181
2.52k
  const SanitizerArgs &Sanitize = getSanitizerArgs();
1182
2.52k
  if (Sanitize.needsAsanRt())
1183
10
    AddLinkSanitizerLibArgs(Args, CmdArgs, "asan");
1184
2.52k
  if (Sanitize.needsLsanRt())
1185
8
    AddLinkSanitizerLibArgs(Args, CmdArgs, "lsan");
1186
2.52k
  if (Sanitize.needsUbsanRt())
1187
33
    AddLinkSanitizerLibArgs(Args, CmdArgs,
1188
33
                            Sanitize.requiresMinimalRuntime() ? 
"ubsan_minimal"2
1189
33
                                                              : 
"ubsan"31
,
1190
33
                            Sanitize.needsSharedRt());
1191
2.52k
  if (Sanitize.needsTsanRt())
1192
5
    AddLinkSanitizerLibArgs(Args, CmdArgs, "tsan");
1193
2.52k
  if (Sanitize.needsFuzzer() && 
!Args.hasArg(options::OPT_dynamiclib)4
) {
1194
3
    AddLinkSanitizerLibArgs(Args, CmdArgs, "fuzzer", /*shared=*/false);
1195
3
1196
3
    // Libfuzzer is written in C++ and requires libcxx.
1197
3
    AddCXXStdlibLibArgs(Args, CmdArgs);
1198
3
  }
1199
2.52k
  if (Sanitize.needsStatsRt()) {
1200
1
    AddLinkRuntimeLib(Args, CmdArgs, "stats_client", RLO_AlwaysLink);
1201
1
    AddLinkSanitizerLibArgs(Args, CmdArgs, "stats");
1202
1
  }
1203
2.52k
1204
2.52k
  const XRayArgs &XRay = getXRayArgs();
1205
2.52k
  if (XRay.needsXRayRt()) {
1206
0
    AddLinkRuntimeLib(Args, CmdArgs, "xray");
1207
0
    AddLinkRuntimeLib(Args, CmdArgs, "xray-basic");
1208
0
    AddLinkRuntimeLib(Args, CmdArgs, "xray-fdr");
1209
0
  }
1210
2.52k
1211
2.52k
  // Otherwise link libSystem, then the dynamic runtime library, and finally any
1212
2.52k
  // target specific static runtime library.
1213
2.52k
  CmdArgs.push_back("-lSystem");
1214
2.52k
1215
2.52k
  // Select the dynamic runtime library and the target specific static library.
1216
2.52k
  if (isTargetIOSBased()) {
1217
2.05k
    // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
1218
2.05k
    // it never went into the SDK.
1219
2.05k
    // Linking against libgcc_s.1 isn't needed for iOS 5.0+
1220
2.05k
    if (isIPhoneOSVersionLT(5, 0) && 
!isTargetIOSSimulator()17
&&
1221
2.05k
        
getTriple().getArch() != llvm::Triple::aarch6410
)
1222
9
      CmdArgs.push_back("-lgcc_s.1");
1223
2.05k
  }
1224
2.52k
  AddLinkRuntimeLib(Args, CmdArgs, "builtins");
1225
2.52k
}
1226
1227
/// Returns the most appropriate macOS target version for the current process.
1228
///
1229
/// If the macOS SDK version is the same or earlier than the system version,
1230
/// then the SDK version is returned. Otherwise the system version is returned.
1231
2.46k
static std::string getSystemOrSDKMacOSVersion(StringRef MacOSSDKVersion) {
1232
2.46k
  unsigned Major, Minor, Micro;
1233
2.46k
  llvm::Triple SystemTriple(llvm::sys::getProcessTriple());
1234
2.46k
  if (!SystemTriple.isMacOSX())
1235
0
    return MacOSSDKVersion;
1236
2.46k
  SystemTriple.getMacOSXVersion(Major, Minor, Micro);
1237
2.46k
  VersionTuple SystemVersion(Major, Minor, Micro);
1238
2.46k
  bool HadExtra;
1239
2.46k
  if (!Driver::GetReleaseVersion(MacOSSDKVersion, Major, Minor, Micro,
1240
2.46k
                                 HadExtra))
1241
0
    return MacOSSDKVersion;
1242
2.46k
  VersionTuple SDKVersion(Major, Minor, Micro);
1243
2.46k
  if (SDKVersion > SystemVersion)
1244
1
    return SystemVersion.getAsString();
1245
2.46k
  return MacOSSDKVersion;
1246
2.46k
}
1247
1248
namespace {
1249
1250
/// The Darwin OS that was selected or inferred from arguments / environment.
1251
struct DarwinPlatform {
1252
  enum SourceKind {
1253
    /// The OS was specified using the -target argument.
1254
    TargetArg,
1255
    /// The OS was specified using the -m<os>-version-min argument.
1256
    OSVersionArg,
1257
    /// The OS was specified using the OS_DEPLOYMENT_TARGET environment.
1258
    DeploymentTargetEnv,
1259
    /// The OS was inferred from the SDK.
1260
    InferredFromSDK,
1261
    /// The OS was inferred from the -arch.
1262
    InferredFromArch
1263
  };
1264
1265
  using DarwinPlatformKind = Darwin::DarwinPlatformKind;
1266
  using DarwinEnvironmentKind = Darwin::DarwinEnvironmentKind;
1267
1268
22.1k
  DarwinPlatformKind getPlatform() const { return Platform; }
1269
1270
22.0k
  DarwinEnvironmentKind getEnvironment() const { return Environment; }
1271
1272
3
  void setEnvironment(DarwinEnvironmentKind Kind) {
1273
3
    Environment = Kind;
1274
3
    InferSimulatorFromArch = false;
1275
3
  }
1276
1277
22.1k
  StringRef getOSVersion() const {
1278
22.1k
    if (Kind == OSVersionArg)
1279
4.62k
      return Argument->getValue();
1280
17.5k
    return OSVersion;
1281
17.5k
  }
1282
1283
7
  void setOSVersion(StringRef S) {
1284
7
    assert(Kind == TargetArg && "Unexpected kind!");
1285
7
    OSVersion = S;
1286
7
  }
1287
1288
14
  bool hasOSVersion() const { return HasOSVersion; }
1289
1290
  /// Returns true if the target OS was explicitly specified.
1291
6
  bool isExplicitlySpecified() const { return Kind <= DeploymentTargetEnv; }
1292
1293
  /// Returns true if the simulator environment can be inferred from the arch.
1294
11.9k
  bool canInferSimulatorFromArch() const { return InferSimulatorFromArch; }
1295
1296
  /// Adds the -m<os>-version-min argument to the compiler invocation.
1297
22.0k
  void addOSVersionMinArgument(DerivedArgList &Args, const OptTable &Opts) {
1298
22.0k
    if (Argument)
1299
4.83k
      return;
1300
17.2k
    assert(Kind != TargetArg && Kind != OSVersionArg && "Invalid kind");
1301
17.2k
    options::ID Opt;
1302
17.2k
    switch (Platform) {
1303
17.2k
    case DarwinPlatformKind::MacOS:
1304
8.58k
      Opt = options::OPT_mmacosx_version_min_EQ;
1305
8.58k
      break;
1306
17.2k
    case DarwinPlatformKind::IPhoneOS:
1307
8.65k
      Opt = options::OPT_miphoneos_version_min_EQ;
1308
8.65k
      break;
1309
17.2k
    case DarwinPlatformKind::TvOS:
1310
7
      Opt = options::OPT_mtvos_version_min_EQ;
1311
7
      break;
1312
17.2k
    case DarwinPlatformKind::WatchOS:
1313
8
      Opt = options::OPT_mwatchos_version_min_EQ;
1314
8
      break;
1315
17.2k
    }
1316
17.2k
    Argument = Args.MakeJoinedArg(nullptr, Opts.getOption(Opt), OSVersion);
1317
17.2k
    Args.append(Argument);
1318
17.2k
  }
1319
1320
  /// Returns the OS version with the argument / environment variable that
1321
  /// specified it.
1322
28
  std::string getAsString(DerivedArgList &Args, const OptTable &Opts) {
1323
28
    switch (Kind) {
1324
28
    case TargetArg:
1325
26
    case OSVersionArg:
1326
26
    case InferredFromSDK:
1327
26
    case InferredFromArch:
1328
26
      assert(Argument && "OS version argument not yet inferred");
1329
26
      return Argument->getAsString(Args);
1330
26
    case DeploymentTargetEnv:
1331
2
      return (llvm::Twine(EnvVarName) + "=" + OSVersion).str();
1332
0
    }
1333
0
    llvm_unreachable("Unsupported Darwin Source Kind");
1334
0
  }
1335
1336
  static DarwinPlatform createFromTarget(const llvm::Triple &TT,
1337
245
                                         StringRef OSVersion, Arg *A) {
1338
245
    DarwinPlatform Result(TargetArg, getPlatformFromOS(TT.getOS()), OSVersion,
1339
245
                          A);
1340
245
    switch (TT.getEnvironment()) {
1341
245
    case llvm::Triple::Simulator:
1342
2
      Result.Environment = DarwinEnvironmentKind::Simulator;
1343
2
      break;
1344
245
    default:
1345
243
      break;
1346
245
    }
1347
245
    unsigned Major, Minor, Micro;
1348
245
    TT.getOSVersion(Major, Minor, Micro);
1349
245
    if (Major == 0)
1350
70
      Result.HasOSVersion = false;
1351
245
    return Result;
1352
245
  }
1353
  static DarwinPlatform createOSVersionArg(DarwinPlatformKind Platform,
1354
4.62k
                                           Arg *A) {
1355
4.62k
    return DarwinPlatform(OSVersionArg, Platform, A);
1356
4.62k
  }
1357
  static DarwinPlatform createDeploymentTargetEnv(DarwinPlatformKind Platform,
1358
                                                  StringRef EnvVarName,
1359
24
                                                  StringRef Value) {
1360
24
    DarwinPlatform Result(DeploymentTargetEnv, Platform, Value);
1361
24
    Result.EnvVarName = EnvVarName;
1362
24
    return Result;
1363
24
  }
1364
  static DarwinPlatform createFromSDK(DarwinPlatformKind Platform,
1365
                                      StringRef Value,
1366
11.0k
                                      bool IsSimulator = false) {
1367
11.0k
    DarwinPlatform Result(InferredFromSDK, Platform, Value);
1368
11.0k
    if (IsSimulator)
1369
4
      Result.Environment = DarwinEnvironmentKind::Simulator;
1370
11.0k
    Result.InferSimulatorFromArch = false;
1371
11.0k
    return Result;
1372
11.0k
  }
1373
  static DarwinPlatform createFromArch(llvm::Triple::OSType OS,
1374
6.14k
                                       StringRef Value) {
1375
6.14k
    return DarwinPlatform(InferredFromArch, getPlatformFromOS(OS), Value);
1376
6.14k
  }
1377
1378
  /// Constructs an inferred SDKInfo value based on the version inferred from
1379
  /// the SDK path itself. Only works for values that were created by inferring
1380
  /// the platform from the SDKPath.
1381
8.62k
  DarwinSDKInfo inferSDKInfo() {
1382
8.62k
    assert(Kind == InferredFromSDK && "can infer SDK info only");
1383
8.62k
    llvm::VersionTuple Version;
1384
8.62k
    bool IsValid = !Version.tryParse(OSVersion);
1385
8.62k
    (void)IsValid;
1386
8.62k
    assert(IsValid && "invalid SDK version");
1387
8.62k
    return DarwinSDKInfo(Version);
1388
8.62k
  }
1389
1390
private:
1391
  DarwinPlatform(SourceKind Kind, DarwinPlatformKind Platform, Arg *Argument)
1392
4.62k
      : Kind(Kind), Platform(Platform), Argument(Argument) {}
1393
  DarwinPlatform(SourceKind Kind, DarwinPlatformKind Platform, StringRef Value,
1394
                 Arg *Argument = nullptr)
1395
17.5k
      : Kind(Kind), Platform(Platform), OSVersion(Value), Argument(Argument) {}
1396
1397
6.38k
  static DarwinPlatformKind getPlatformFromOS(llvm::Triple::OSType OS) {
1398
6.38k
    switch (OS) {
1399
6.38k
    case llvm::Triple::Darwin:
1400
6.23k
    case llvm::Triple::MacOSX:
1401
6.23k
      return DarwinPlatformKind::MacOS;
1402
6.23k
    case llvm::Triple::IOS:
1403
109
      return DarwinPlatformKind::IPhoneOS;
1404
6.23k
    case llvm::Triple::TvOS:
1405
17
      return DarwinPlatformKind::TvOS;
1406
6.23k
    case llvm::Triple::WatchOS:
1407
21
      return DarwinPlatformKind::WatchOS;
1408
6.23k
    default:
1409
0
      llvm_unreachable("Unable to infer Darwin variant");
1410
6.38k
    }
1411
6.38k
  }
1412
1413
  SourceKind Kind;
1414
  DarwinPlatformKind Platform;
1415
  DarwinEnvironmentKind Environment = DarwinEnvironmentKind::NativeEnvironment;
1416
  std::string OSVersion;
1417
  bool HasOSVersion = true, InferSimulatorFromArch = true;
1418
  Arg *Argument;
1419
  StringRef EnvVarName;
1420
};
1421
1422
/// Returns the deployment target that's specified using the -m<os>-version-min
1423
/// argument.
1424
Optional<DarwinPlatform>
1425
getDeploymentTargetFromOSVersionArg(DerivedArgList &Args,
1426
22.0k
                                    const Driver &TheDriver) {
1427
22.0k
  Arg *OSXVersion = Args.getLastArg(options::OPT_mmacosx_version_min_EQ);
1428
22.0k
  Arg *iOSVersion = Args.getLastArg(options::OPT_miphoneos_version_min_EQ,
1429
22.0k
                                    options::OPT_mios_simulator_version_min_EQ);
1430
22.0k
  Arg *TvOSVersion =
1431
22.0k
      Args.getLastArg(options::OPT_mtvos_version_min_EQ,
1432
22.0k
                      options::OPT_mtvos_simulator_version_min_EQ);
1433
22.0k
  Arg *WatchOSVersion =
1434
22.0k
      Args.getLastArg(options::OPT_mwatchos_version_min_EQ,
1435
22.0k
                      options::OPT_mwatchos_simulator_version_min_EQ);
1436
22.0k
  if (OSXVersion) {
1437
1.41k
    if (iOSVersion || TvOSVersion || WatchOSVersion) {
1438
0
      TheDriver.Diag(diag::err_drv_argument_not_allowed_with)
1439
0
          << OSXVersion->getAsString(Args)
1440
0
          << (iOSVersion ? iOSVersion
1441
0
                         : TvOSVersion ? TvOSVersion : WatchOSVersion)
1442
0
                 ->getAsString(Args);
1443
0
    }
1444
1.41k
    return DarwinPlatform::createOSVersionArg(Darwin::MacOS, OSXVersion);
1445
20.6k
  } else if (iOSVersion) {
1446
3.16k
    if (TvOSVersion || WatchOSVersion) {
1447
0
      TheDriver.Diag(diag::err_drv_argument_not_allowed_with)
1448
0
          << iOSVersion->getAsString(Args)
1449
0
          << (TvOSVersion ? TvOSVersion : WatchOSVersion)->getAsString(Args);
1450
0
    }
1451
3.16k
    return DarwinPlatform::createOSVersionArg(Darwin::IPhoneOS, iOSVersion);
1452
17.5k
  } else if (TvOSVersion) {
1453
19
    if (WatchOSVersion) {
1454
0
      TheDriver.Diag(diag::err_drv_argument_not_allowed_with)
1455
0
          << TvOSVersion->getAsString(Args)
1456
0
          << WatchOSVersion->getAsString(Args);
1457
0
    }
1458
19
    return DarwinPlatform::createOSVersionArg(Darwin::TvOS, TvOSVersion);
1459
17.4k
  } else if (WatchOSVersion)
1460
22
    return DarwinPlatform::createOSVersionArg(Darwin::WatchOS, WatchOSVersion);
1461
17.4k
  return None;
1462
17.4k
}
1463
1464
/// Returns the deployment target that's specified using the
1465
/// OS_DEPLOYMENT_TARGET environment variable.
1466
Optional<DarwinPlatform>
1467
getDeploymentTargetFromEnvironmentVariables(const Driver &TheDriver,
1468
17.2k
                                            const llvm::Triple &Triple) {
1469
17.2k
  std::string Targets[Darwin::LastDarwinPlatform + 1];
1470
17.2k
  const char *EnvVars[] = {
1471
17.2k
      "MACOSX_DEPLOYMENT_TARGET",
1472
17.2k
      "IPHONEOS_DEPLOYMENT_TARGET",
1473
17.2k
      "TVOS_DEPLOYMENT_TARGET",
1474
17.2k
      "WATCHOS_DEPLOYMENT_TARGET",
1475
17.2k
  };
1476
17.2k
  static_assert(llvm::array_lengthof(EnvVars) == Darwin::LastDarwinPlatform + 1,
1477
17.2k
                "Missing platform");
1478
69.0k
  for (const auto &I : llvm::enumerate(llvm::makeArrayRef(EnvVars))) {
1479
69.0k
    if (char *Env = ::getenv(I.value()))
1480
28
      Targets[I.index()] = Env;
1481
69.0k
  }
1482
17.2k
1483
17.2k
  // Do not allow conflicts with the watchOS target.
1484
17.2k
  if (!Targets[Darwin::WatchOS].empty() &&
1485
17.2k
      
(5
!Targets[Darwin::IPhoneOS].empty()5
||
!Targets[Darwin::TvOS].empty()5
)) {
1486
0
    TheDriver.Diag(diag::err_drv_conflicting_deployment_targets)
1487
0
        << "WATCHOS_DEPLOYMENT_TARGET"
1488
0
        << (!Targets[Darwin::IPhoneOS].empty() ? "IPHONEOS_DEPLOYMENT_TARGET"
1489
0
                                               : "TVOS_DEPLOYMENT_TARGET");
1490
0
  }
1491
17.2k
1492
17.2k
  // Do not allow conflicts with the tvOS target.
1493
17.2k
  if (!Targets[Darwin::TvOS].empty() && 
!Targets[Darwin::IPhoneOS].empty()5
) {
1494
0
    TheDriver.Diag(diag::err_drv_conflicting_deployment_targets)
1495
0
        << "TVOS_DEPLOYMENT_TARGET"
1496
0
        << "IPHONEOS_DEPLOYMENT_TARGET";
1497
0
  }
1498
17.2k
1499
17.2k
  // Allow conflicts among OSX and iOS for historical reasons, but choose the
1500
17.2k
  // default platform.
1501
17.2k
  if (!Targets[Darwin::MacOS].empty() &&
1502
17.2k
      
(7
!Targets[Darwin::IPhoneOS].empty()7
||
1503
7
       
!Targets[Darwin::WatchOS].empty()5
||
!Targets[Darwin::TvOS].empty()4
)) {
1504
4
    if (Triple.getArch() == llvm::Triple::arm ||
1505
4
        
Triple.getArch() == llvm::Triple::aarch641
||
1506
4
        
Triple.getArch() == llvm::Triple::thumb1
)
1507
3
      Targets[Darwin::MacOS] = "";
1508
1
    else
1509
1
      Targets[Darwin::IPhoneOS] = Targets[Darwin::WatchOS] =
1510
1
          Targets[Darwin::TvOS] = "";
1511
4
  }
1512
17.2k
1513
68.9k
  for (const auto &Target : llvm::enumerate(llvm::makeArrayRef(Targets))) {
1514
68.9k
    if (!Target.value().empty())
1515
24
      return DarwinPlatform::createDeploymentTargetEnv(
1516
24
          (Darwin::DarwinPlatformKind)Target.index(), EnvVars[Target.index()],
1517
24
          Target.value());
1518
68.9k
  }
1519
17.2k
  
return None17.2k
;
1520
17.2k
}
1521
1522
/// Tries to infer the deployment target from the SDK specified by -isysroot
1523
/// (or SDKROOT). Uses the version specified in the SDKSettings.json file if
1524
/// it's available.
1525
Optional<DarwinPlatform>
1526
inferDeploymentTargetFromSDK(DerivedArgList &Args,
1527
17.2k
                             const Optional<DarwinSDKInfo> &SDKInfo) {
1528
17.2k
  const Arg *A = Args.getLastArg(options::OPT_isysroot);
1529
17.2k
  if (!A)
1530
6.11k
    return None;
1531
11.1k
  StringRef isysroot = A->getValue();
1532
11.1k
  StringRef SDK = Darwin::getSDKName(isysroot);
1533
11.1k
  if (!SDK.size())
1534
48
    return None;
1535
11.0k
1536
11.0k
  std::string Version;
1537
11.0k
  if (SDKInfo) {
1538
2.46k
    // Get the version from the SDKSettings.json if it's available.
1539
2.46k
    Version = SDKInfo->getVersion().getAsString();
1540
8.63k
  } else {
1541
8.63k
    // Slice the version number out.
1542
8.63k
    // Version number is between the first and the last number.
1543
8.63k
    size_t StartVer = SDK.find_first_of("0123456789");
1544
8.63k
    size_t EndVer = SDK.find_last_of("0123456789");
1545
8.63k
    if (StartVer != StringRef::npos && EndVer > StartVer)
1546
8.63k
      Version = SDK.slice(StartVer, EndVer + 1);
1547
8.63k
  }
1548
11.0k
  if (Version.empty())
1549
0
    return None;
1550
11.0k
1551
11.0k
  if (SDK.startswith("iPhoneOS") || 
SDK.startswith("iPhoneSimulator")2.47k
)
1552
8.62k
    return DarwinPlatform::createFromSDK(
1553
8.62k
        Darwin::IPhoneOS, Version,
1554
8.62k
        /*IsSimulator=*/SDK.startswith("iPhoneSimulator"));
1555
2.47k
  else if (SDK.startswith("MacOSX"))
1556
2.46k
    return DarwinPlatform::createFromSDK(Darwin::MacOS,
1557
2.46k
                                         getSystemOrSDKMacOSVersion(Version));
1558
5
  else if (SDK.startswith("WatchOS") || 
SDK.startswith("WatchSimulator")3
)
1559
3
    return DarwinPlatform::createFromSDK(
1560
3
        Darwin::WatchOS, Version,
1561
3
        /*IsSimulator=*/SDK.startswith("WatchSimulator"));
1562
2
  else if (SDK.startswith("AppleTVOS") || 
SDK.startswith("AppleTVSimulator")1
)
1563
2
    return DarwinPlatform::createFromSDK(
1564
2
        Darwin::TvOS, Version,
1565
2
        /*IsSimulator=*/SDK.startswith("AppleTVSimulator"));
1566
0
  return None;
1567
0
}
1568
1569
std::string getOSVersion(llvm::Triple::OSType OS, const llvm::Triple &Triple,
1570
6.38k
                         const Driver &TheDriver) {
1571
6.38k
  unsigned Major, Minor, Micro;
1572
6.38k
  llvm::Triple SystemTriple(llvm::sys::getProcessTriple());
1573
6.38k
  switch (OS) {
1574
6.38k
  case llvm::Triple::Darwin:
1575
6.23k
  case llvm::Triple::MacOSX:
1576
6.23k
    // If there is no version specified on triple, and both host and target are
1577
6.23k
    // macos, use the host triple to infer OS version.
1578
6.23k
    if (Triple.isMacOSX() && SystemTriple.isMacOSX() &&
1579
6.23k
        !Triple.getOSMajorVersion())
1580
148
      SystemTriple.getMacOSXVersion(Major, Minor, Micro);
1581
6.09k
    else if (!Triple.getMacOSXVersion(Major, Minor, Micro))
1582
0
      TheDriver.Diag(diag::err_drv_invalid_darwin_version)
1583
0
          << Triple.getOSName();
1584
6.23k
    break;
1585
6.23k
  case llvm::Triple::IOS:
1586
109
    Triple.getiOSVersion(Major, Minor, Micro);
1587
109
    break;
1588
6.23k
  case llvm::Triple::TvOS:
1589
17
    Triple.getOSVersion(Major, Minor, Micro);
1590
17
    break;
1591
6.23k
  case llvm::Triple::WatchOS:
1592
21
    Triple.getWatchOSVersion(Major, Minor, Micro);
1593
21
    break;
1594
6.23k
  default:
1595
0
    llvm_unreachable("Unexpected OS type");
1596
6.23k
    
break0
;
1597
6.38k
  }
1598
6.38k
1599
6.38k
  std::string OSVersion;
1600
6.38k
  llvm::raw_string_ostream(OSVersion) << Major << '.' << Minor << '.' << Micro;
1601
6.38k
  return OSVersion;
1602
6.38k
}
1603
1604
/// Tries to infer the target OS from the -arch.
1605
Optional<DarwinPlatform>
1606
inferDeploymentTargetFromArch(DerivedArgList &Args, const Darwin &Toolchain,
1607
                              const llvm::Triple &Triple,
1608
6.14k
                              const Driver &TheDriver) {
1609
6.14k
  llvm::Triple::OSType OSTy = llvm::Triple::UnknownOS;
1610
6.14k
1611
6.14k
  StringRef MachOArchName = Toolchain.getMachOArchName(Args);
1612
6.14k
  if (MachOArchName == "armv7" || 
MachOArchName == "armv7s"6.12k
||
1613
6.14k
      
MachOArchName == "arm64"6.12k
)
1614
25
    OSTy = llvm::Triple::IOS;
1615
6.11k
  else if (MachOArchName == "armv7k")
1616
1
    OSTy = llvm::Triple::WatchOS;
1617
6.11k
  else if (MachOArchName != "armv6m" && MachOArchName != "armv7m" &&
1618
6.11k
           MachOArchName != "armv7em")
1619
6.11k
    OSTy = llvm::Triple::MacOSX;
1620
6.14k
1621
6.14k
  if (OSTy == llvm::Triple::UnknownOS)
1622
0
    return None;
1623
6.14k
  return DarwinPlatform::createFromArch(OSTy,
1624
6.14k
                                        getOSVersion(OSTy, Triple, TheDriver));
1625
6.14k
}
1626
1627
/// Returns the deployment target that's specified using the -target option.
1628
Optional<DarwinPlatform> getDeploymentTargetFromTargetArg(
1629
22.0k
    DerivedArgList &Args, const llvm::Triple &Triple, const Driver &TheDriver) {
1630
22.0k
  if (!Args.hasArg(options::OPT_target))
1631
20.8k
    return None;
1632
1.20k
  if (Triple.getOS() == llvm::Triple::Darwin ||
1633
1.20k
      
Triple.getOS() == llvm::Triple::UnknownOS245
)
1634
962
    return None;
1635
245
  std::string OSVersion = getOSVersion(Triple.getOS(), Triple, TheDriver);
1636
245
  return DarwinPlatform::createFromTarget(Triple, OSVersion,
1637
245
                                          Args.getLastArg(options::OPT_target));
1638
245
}
1639
1640
Optional<DarwinSDKInfo> parseSDKSettings(llvm::vfs::FileSystem &VFS,
1641
                                         const ArgList &Args,
1642
22.0k
                                         const Driver &TheDriver) {
1643
22.0k
  const Arg *A = Args.getLastArg(options::OPT_isysroot);
1644
22.0k
  if (!A)
1645
6.40k
    return None;
1646
15.6k
  StringRef isysroot = A->getValue();
1647
15.6k
  auto SDKInfoOrErr = driver::parseDarwinSDKInfo(VFS, isysroot);
1648
15.6k
  if (!SDKInfoOrErr) {
1649
2
    llvm::consumeError(SDKInfoOrErr.takeError());
1650
2
    TheDriver.Diag(diag::warn_drv_darwin_sdk_invalid_settings);
1651
2
    return None;
1652
2
  }
1653
15.6k
  return *SDKInfoOrErr;
1654
15.6k
}
1655
1656
} // namespace
1657
1658
22.0k
void Darwin::AddDeploymentTarget(DerivedArgList &Args) const {
1659
22.0k
  const OptTable &Opts = getDriver().getOpts();
1660
22.0k
1661
22.0k
  // Support allowing the SDKROOT environment variable used by xcrun and other
1662
22.0k
  // Xcode tools to define the default sysroot, by making it the default for
1663
22.0k
  // isysroot.
1664
22.0k
  if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
1665
14.0k
    // Warn if the path does not exist.
1666
14.0k
    if (!getVFS().exists(A->getValue()))
1667
22
      getDriver().Diag(clang::diag::warn_missing_sysroot) << A->getValue();
1668
14.0k
  } else {
1669
8.01k
    if (char *env = ::getenv("SDKROOT")) {
1670
1.61k
      // We only use this value as the default if it is an absolute path,
1671
1.61k
      // exists, and it is not the root path.
1672
1.61k
      if (llvm::sys::path::is_absolute(env) && getVFS().exists(env) &&
1673
1.61k
          
StringRef(env) != "/"1.61k
) {
1674
1.60k
        Args.append(Args.MakeSeparateArg(
1675
1.60k
            nullptr, Opts.getOption(options::OPT_isysroot), env));
1676
1.60k
      }
1677
1.61k
    }
1678
8.01k
  }
1679
22.0k
1680
22.0k
  // Read the SDKSettings.json file for more information, like the SDK version
1681
22.0k
  // that we can pass down to the compiler.
1682
22.0k
  SDKInfo = parseSDKSettings(getVFS(), Args, getDriver());
1683
22.0k
1684
22.0k
  // The OS and the version can be specified using the -target argument.
1685
22.0k
  Optional<DarwinPlatform> OSTarget =
1686
22.0k
      getDeploymentTargetFromTargetArg(Args, getTriple(), getDriver());
1687
22.0k
  if (OSTarget) {
1688
245
    Optional<DarwinPlatform> OSVersionArgTarget =
1689
245
        getDeploymentTargetFromOSVersionArg(Args, getDriver());
1690
245
    if (OSVersionArgTarget) {
1691
34
      unsigned TargetMajor, TargetMinor, TargetMicro;
1692
34
      bool TargetExtra;
1693
34
      unsigned ArgMajor, ArgMinor, ArgMicro;
1694
34
      bool ArgExtra;
1695
34
      if (OSTarget->getPlatform() != OSVersionArgTarget->getPlatform() ||
1696
34
          
(31
Driver::GetReleaseVersion(OSTarget->getOSVersion(), TargetMajor,
1697
31
                                     TargetMinor, TargetMicro, TargetExtra) &&
1698
31
           Driver::GetReleaseVersion(OSVersionArgTarget->getOSVersion(),
1699
31
                                     ArgMajor, ArgMinor, ArgMicro, ArgExtra) &&
1700
31
           (VersionTuple(TargetMajor, TargetMinor, TargetMicro) !=
1701
31
                VersionTuple(ArgMajor, ArgMinor, ArgMicro) ||
1702
31
            
TargetExtra != ArgExtra18
))) {
1703
17
        // Select the OS version from the -m<os>-version-min argument when
1704
17
        // the -target does not include an OS version.
1705
17
        if (OSTarget->getPlatform() == OSVersionArgTarget->getPlatform() &&
1706
17
            
!OSTarget->hasOSVersion()14
) {
1707
7
          OSTarget->setOSVersion(OSVersionArgTarget->getOSVersion());
1708
10
        } else {
1709
10
          // Warn about -m<os>-version-min that doesn't match the OS version
1710
10
          // that's specified in the target.
1711
10
          std::string OSVersionArg =
1712
10
              OSVersionArgTarget->getAsString(Args, Opts);
1713
10
          std::string TargetArg = OSTarget->getAsString(Args, Opts);
1714
10
          getDriver().Diag(clang::diag::warn_drv_overriding_flag_option)
1715
10
              << OSVersionArg << TargetArg;
1716
10
        }
1717
17
      }
1718
34
    }
1719
21.8k
  } else {
1720
21.8k
    // The OS target can be specified using the -m<os>version-min argument.
1721
21.8k
    OSTarget = getDeploymentTargetFromOSVersionArg(Args, getDriver());
1722
21.8k
    // If no deployment target was specified on the command line, check for
1723
21.8k
    // environment defines.
1724
21.8k
    if (!OSTarget) {
1725
17.2k
      OSTarget =
1726
17.2k
          getDeploymentTargetFromEnvironmentVariables(getDriver(), getTriple());
1727
17.2k
      if (OSTarget) {
1728
24
        // Don't infer simulator from the arch when the SDK is also specified.
1729
24
        Optional<DarwinPlatform> SDKTarget =
1730
24
            inferDeploymentTargetFromSDK(Args, SDKInfo);
1731
24
        if (SDKTarget)
1732
3
          OSTarget->setEnvironment(SDKTarget->getEnvironment());
1733
24
      }
1734
17.2k
    }
1735
21.8k
    // If there is no command-line argument to specify the Target version and
1736
21.8k
    // no environment variable defined, see if we can set the default based
1737
21.8k
    // on -isysroot using SDKSettings.json if it exists.
1738
21.8k
    if (!OSTarget) {
1739
17.2k
      OSTarget = inferDeploymentTargetFromSDK(Args, SDKInfo);
1740
17.2k
      /// If the target was successfully constructed from the SDK path, try to
1741
17.2k
      /// infer the SDK info if the SDK doesn't have it.
1742
17.2k
      if (OSTarget && 
!SDKInfo11.0k
)
1743
8.62k
        SDKInfo = OSTarget->inferSDKInfo();
1744
17.2k
    }
1745
21.8k
    // If no OS targets have been specified, try to guess platform from -target
1746
21.8k
    // or arch name and compute the version from the triple.
1747
21.8k
    if (!OSTarget)
1748
6.14k
      OSTarget =
1749
6.14k
          inferDeploymentTargetFromArch(Args, *this, getTriple(), getDriver());
1750
21.8k
  }
1751
22.0k
1752
22.0k
  assert(OSTarget && "Unable to infer Darwin variant");
1753
22.0k
  OSTarget->addOSVersionMinArgument(Args, Opts);
1754
22.0k
  DarwinPlatformKind Platform = OSTarget->getPlatform();
1755
22.0k
1756
22.0k
  unsigned Major, Minor, Micro;
1757
22.0k
  bool HadExtra;
1758
22.0k
  // Set the tool chain target information.
1759
22.0k
  if (Platform == MacOS) {
1760
10.1k
    if (!Driver::GetReleaseVersion(OSTarget->getOSVersion(), Major, Minor,
1761
10.1k
                                   Micro, HadExtra) ||
1762
10.1k
        
HadExtra10.1k
||
Major != 1010.1k
||
Minor >= 10010.1k
||
Micro >= 10010.1k
)
1763
3
      getDriver().Diag(diag::err_drv_invalid_version_number)
1764
3
          << OSTarget->getAsString(Args, Opts);
1765
11.9k
  } else if (Platform == IPhoneOS) {
1766
11.8k
    if (!Driver::GetReleaseVersion(OSTarget->getOSVersion(), Major, Minor,
1767
11.8k
                                   Micro, HadExtra) ||
1768
11.8k
        HadExtra || Major >= 100 || Minor >= 100 || Micro >= 100)
1769
0
      getDriver().Diag(diag::err_drv_invalid_version_number)
1770
0
          << OSTarget->getAsString(Args, Opts);
1771
11.8k
    ;
1772
11.8k
    // For 32-bit targets, the deployment target for iOS has to be earlier than
1773
11.8k
    // iOS 11.
1774
11.8k
    if (getTriple().isArch32Bit() && 
Major >= 112.04k
) {
1775
6
      // If the deployment target is explicitly specified, print a diagnostic.
1776
6
      if (OSTarget->isExplicitlySpecified()) {
1777
5
        getDriver().Diag(diag::warn_invalid_ios_deployment_target)
1778
5
            << OSTarget->getAsString(Args, Opts);
1779
5
        // Otherwise, set it to 10.99.99.
1780
5
      } else {
1781
1
        Major = 10;
1782
1
        Minor = 99;
1783
1
        Micro = 99;
1784
1
      }
1785
6
    }
1786
11.8k
  } else 
if (80
Platform == TvOS80
) {
1787
37
    if (!Driver::GetReleaseVersion(OSTarget->getOSVersion(), Major, Minor,
1788
37
                                   Micro, HadExtra) ||
1789
37
        HadExtra || Major >= 100 || Minor >= 100 || Micro >= 100)
1790
0
      getDriver().Diag(diag::err_drv_invalid_version_number)
1791
0
          << OSTarget->getAsString(Args, Opts);
1792
43
  } else if (Platform == WatchOS) {
1793
43
    if (!Driver::GetReleaseVersion(OSTarget->getOSVersion(), Major, Minor,
1794
43
                                   Micro, HadExtra) ||
1795
43
        HadExtra || Major >= 10 || Minor >= 100 || Micro >= 100)
1796
0
      getDriver().Diag(diag::err_drv_invalid_version_number)
1797
0
          << OSTarget->getAsString(Args, Opts);
1798
43
  } else
1799
43
    
llvm_unreachable0
("unknown kind of Darwin platform");
1800
22.0k
1801
22.0k
  DarwinEnvironmentKind Environment = OSTarget->getEnvironment();
1802
22.0k
  // Recognize iOS targets with an x86 architecture as the iOS simulator.
1803
22.0k
  if (Environment == NativeEnvironment && 
Platform != MacOS22.0k
&&
1804
22.0k
      
OSTarget->canInferSimulatorFromArch()11.9k
&&
1805
22.0k
      
(3.33k
getTriple().getArch() == llvm::Triple::x863.33k
||
1806
3.33k
       
getTriple().getArch() == llvm::Triple::x86_642.96k
))
1807
927
    Environment = Simulator;
1808
22.0k
1809
22.0k
  setTarget(Platform, Environment, Major, Minor, Micro);
1810
22.0k
1811
22.0k
  if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
1812
15.6k
    StringRef SDK = getSDKName(A->getValue());
1813
15.6k
    if (SDK.size() > 0) {
1814
15.6k
      size_t StartVer = SDK.find_first_of("0123456789");
1815
15.6k
      StringRef SDKName = SDK.slice(0, StartVer);
1816
15.6k
      if (!SDKName.startswith(getPlatformFamily()))
1817
16
        getDriver().Diag(diag::warn_incompatible_sysroot)
1818
16
            << SDKName << getPlatformFamily();
1819
15.6k
    }
1820
15.6k
  }
1821
22.0k
}
1822
1823
// Returns the effective header sysroot path to use. This comes either from
1824
// -isysroot or --sysroot.
1825
24.5k
llvm::StringRef DarwinClang::GetHeaderSysroot(const llvm::opt::ArgList &DriverArgs) const {
1826
24.5k
  if(DriverArgs.hasArg(options::OPT_isysroot))
1827
13.1k
    return DriverArgs.getLastArgValue(options::OPT_isysroot);
1828
11.4k
  if (!getDriver().SysRoot.empty())
1829
30
    return getDriver().SysRoot;
1830
11.4k
  return "/";
1831
11.4k
}
1832
1833
void DarwinClang::AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
1834
16.0k
                                            llvm::opt::ArgStringList &CC1Args) const {
1835
16.0k
  const Driver &D = getDriver();
1836
16.0k
1837
16.0k
  llvm::StringRef Sysroot = GetHeaderSysroot(DriverArgs);
1838
16.0k
1839
16.0k
  bool NoStdInc = DriverArgs.hasArg(options::OPT_nostdinc);
1840
16.0k
  bool NoStdlibInc = DriverArgs.hasArg(options::OPT_nostdlibinc);
1841
16.0k
  bool NoBuiltinInc = DriverArgs.hasArg(options::OPT_nobuiltininc);
1842
16.0k
1843
16.0k
  // Add <sysroot>/usr/local/include
1844
16.0k
  if (!NoStdInc && 
!NoStdlibInc15.9k
) {
1845
15.9k
      SmallString<128> P(Sysroot);
1846
15.9k
      llvm::sys::path::append(P, "usr", "local", "include");
1847
15.9k
      addSystemInclude(DriverArgs, CC1Args, P);
1848
15.9k
  }
1849
16.0k
1850
16.0k
  // Add the Clang builtin headers (<resource>/include)
1851
16.0k
  if (!NoStdInc && 
!NoBuiltinInc15.9k
) {
1852
15.9k
    SmallString<128> P(D.ResourceDir);
1853
15.9k
    llvm::sys::path::append(P, "include");
1854
15.9k
    addSystemInclude(DriverArgs, CC1Args, P);
1855
15.9k
  }
1856
16.0k
1857
16.0k
  if (NoStdInc || 
NoStdlibInc15.9k
)
1858
16
    return;
1859
15.9k
1860
15.9k
  // Check for configure-time C include directories.
1861
15.9k
  llvm::StringRef CIncludeDirs(C_INCLUDE_DIRS);
1862
15.9k
  if (!CIncludeDirs.empty()) {
1863
0
    llvm::SmallVector<llvm::StringRef, 5> dirs;
1864
0
    CIncludeDirs.split(dirs, ":");
1865
0
    for (llvm::StringRef dir : dirs) {
1866
0
      llvm::StringRef Prefix =
1867
0
          llvm::sys::path::is_absolute(dir) ? llvm::StringRef(Sysroot) : "";
1868
0
      addExternCSystemInclude(DriverArgs, CC1Args, Prefix + dir);
1869
0
    }
1870
15.9k
  } else {
1871
15.9k
    // Otherwise, add <sysroot>/usr/include.
1872
15.9k
    SmallString<128> P(Sysroot);
1873
15.9k
    llvm::sys::path::append(P, "usr", "include");
1874
15.9k
    addExternCSystemInclude(DriverArgs, CC1Args, P.str());
1875
15.9k
  }
1876
15.9k
}
1877
1878
bool DarwinClang::AddGnuCPlusPlusIncludePaths(const llvm::opt::ArgList &DriverArgs,
1879
                                              llvm::opt::ArgStringList &CC1Args,
1880
                                              llvm::SmallString<128> Base,
1881
                                              llvm::StringRef Version,
1882
                                              llvm::StringRef ArchDir,
1883
45
                                              llvm::StringRef BitDir) const {
1884
45
  llvm::sys::path::append(Base, Version);
1885
45
1886
45
  // Add the base dir
1887
45
  addSystemInclude(DriverArgs, CC1Args, Base);
1888
45
1889
45
  // Add the multilib dirs
1890
45
  {
1891
45
    llvm::SmallString<128> P = Base;
1892
45
    if (!ArchDir.empty())
1893
45
      llvm::sys::path::append(P, ArchDir);
1894
45
    if (!BitDir.empty())
1895
20
      llvm::sys::path::append(P, BitDir);
1896
45
    addSystemInclude(DriverArgs, CC1Args, P);
1897
45
  }
1898
45
1899
45
  // Add the backward dir
1900
45
  {
1901
45
    llvm::SmallString<128> P = Base;
1902
45
    llvm::sys::path::append(P, "backward");
1903
45
    addSystemInclude(DriverArgs, CC1Args, P);
1904
45
  }
1905
45
1906
45
  return getVFS().exists(Base);
1907
45
}
1908
1909
void DarwinClang::AddClangCXXStdlibIncludeArgs(
1910
    const llvm::opt::ArgList &DriverArgs,
1911
8.57k
    llvm::opt::ArgStringList &CC1Args) const {
1912
8.57k
  // The implementation from a base class will pass through the -stdlib to
1913
8.57k
  // CC1Args.
1914
8.57k
  // FIXME: this should not be necessary, remove usages in the frontend
1915
8.57k
  //        (e.g. HeaderSearchOptions::UseLibcxx) and don't pipe -stdlib.
1916
8.57k
  //        Also check whether this is used for setting library search paths.
1917
8.57k
  ToolChain::AddClangCXXStdlibIncludeArgs(DriverArgs, CC1Args);
1918
8.57k
1919
8.57k
  if (DriverArgs.hasArg(options::OPT_nostdlibinc) ||
1920
8.57k
      
DriverArgs.hasArg(options::OPT_nostdincxx)8.57k
)
1921
2
    return;
1922
8.57k
1923
8.57k
  llvm::StringRef Sysroot = GetHeaderSysroot(DriverArgs);
1924
8.57k
1925
8.57k
  switch (GetCXXStdlibType(DriverArgs)) {
1926
8.57k
  case ToolChain::CST_Libcxx: {
1927
8.54k
    // On Darwin, libc++ is installed alongside the compiler in
1928
8.54k
    // include/c++/v1, so get from '<install>/bin' to '<install>/include/c++/v1'.
1929
8.54k
    {
1930
8.54k
      llvm::SmallString<128> P = llvm::StringRef(getDriver().getInstalledDir());
1931
8.54k
      // Note that P can be relative, so we have to '..' and not parent_path.
1932
8.54k
      llvm::sys::path::append(P, "..", "include", "c++", "v1");
1933
8.54k
      addSystemInclude(DriverArgs, CC1Args, P);
1934
8.54k
    }
1935
8.54k
    // Also add <sysroot>/usr/include/c++/v1 unless -nostdinc is used,
1936
8.54k
    // to match the legacy behavior in CC1.
1937
8.54k
    if (!DriverArgs.hasArg(options::OPT_nostdinc)) {
1938
8.54k
      llvm::SmallString<128> P = Sysroot;
1939
8.54k
      llvm::sys::path::append(P, "usr", "include", "c++", "v1");
1940
8.54k
      addSystemInclude(DriverArgs, CC1Args, P);
1941
8.54k
    }
1942
8.54k
    break;
1943
8.57k
  }
1944
8.57k
1945
8.57k
  case ToolChain::CST_Libstdcxx:
1946
23
    llvm::SmallString<128> UsrIncludeCxx = Sysroot;
1947
23
    llvm::sys::path::append(UsrIncludeCxx, "usr", "include", "c++");
1948
23
1949
23
    llvm::Triple::ArchType arch = getTriple().getArch();
1950
23
    bool IsBaseFound = true;
1951
23
    switch (arch) {
1952
23
    
default: break0
;
1953
23
1954
23
    case llvm::Triple::ppc:
1955
2
    case llvm::Triple::ppc64:
1956
2
      IsBaseFound = AddGnuCPlusPlusIncludePaths(DriverArgs, CC1Args, UsrIncludeCxx,
1957
2
                                                "4.2.1",
1958
2
                                                "powerpc-apple-darwin10",
1959
2
                                                arch == llvm::Triple::ppc64 ? 
"ppc64"1
:
""1
);
1960
2
      IsBaseFound |= AddGnuCPlusPlusIncludePaths(DriverArgs, CC1Args, UsrIncludeCxx,
1961
2
                                                "4.0.0", "powerpc-apple-darwin10",
1962
2
                                                 arch == llvm::Triple::ppc64 ? 
"ppc64"1
:
""1
);
1963
2
      break;
1964
2
1965
17
    case llvm::Triple::x86:
1966
17
    case llvm::Triple::x86_64:
1967
17
      IsBaseFound = AddGnuCPlusPlusIncludePaths(DriverArgs, CC1Args, UsrIncludeCxx,
1968
17
                                                "4.2.1",
1969
17
                                                "i686-apple-darwin10",
1970
17
                                                arch == llvm::Triple::x86_64 ? 
"x86_64"12
:
""5
);
1971
17
      IsBaseFound |= AddGnuCPlusPlusIncludePaths(DriverArgs, CC1Args, UsrIncludeCxx,
1972
17
                                                "4.0.0", "i686-apple-darwin8",
1973
17
                                                 "");
1974
17
      break;
1975
17
1976
17
    case llvm::Triple::arm:
1977
3
    case llvm::Triple::thumb:
1978
3
      IsBaseFound = AddGnuCPlusPlusIncludePaths(DriverArgs, CC1Args, UsrIncludeCxx,
1979
3
                                                "4.2.1",
1980
3
                                                "arm-apple-darwin10",
1981
3
                                                "v7");
1982
3
      IsBaseFound |= AddGnuCPlusPlusIncludePaths(DriverArgs, CC1Args, UsrIncludeCxx,
1983
3
                                                "4.2.1",
1984
3
                                                "arm-apple-darwin10",
1985
3
                                                 "v6");
1986
3
      break;
1987
3
1988
3
    case llvm::Triple::aarch64:
1989
1
      IsBaseFound = AddGnuCPlusPlusIncludePaths(DriverArgs, CC1Args, UsrIncludeCxx,
1990
1
                                                "4.2.1",
1991
1
                                                "arm64-apple-darwin10",
1992
1
                                                "");
1993
1
      break;
1994
23
    }
1995
23
1996
23
    if (!IsBaseFound) {
1997
16
      getDriver().Diag(diag::warn_drv_libstdcxx_not_found);
1998
16
    }
1999
23
2000
23
    break;
2001
8.57k
  }
2002
8.57k
}
2003
void DarwinClang::AddCXXStdlibLibArgs(const ArgList &Args,
2004
1.78k
                                      ArgStringList &CmdArgs) const {
2005
1.78k
  CXXStdlibType Type = GetCXXStdlibType(Args);
2006
1.78k
2007
1.78k
  switch (Type) {
2008
1.78k
  case ToolChain::CST_Libcxx:
2009
1.78k
    CmdArgs.push_back("-lc++");
2010
1.78k
    break;
2011
1.78k
2012
1.78k
  case ToolChain::CST_Libstdcxx:
2013
2
    // Unfortunately, -lstdc++ doesn't always exist in the standard search path;
2014
2
    // it was previously found in the gcc lib dir. However, for all the Darwin
2015
2
    // platforms we care about it was -lstdc++.6, so we search for that
2016
2
    // explicitly if we can't see an obvious -lstdc++ candidate.
2017
2
2018
2
    // Check in the sysroot first.
2019
2
    if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
2020
0
      SmallString<128> P(A->getValue());
2021
0
      llvm::sys::path::append(P, "usr", "lib", "libstdc++.dylib");
2022
0
2023
0
      if (!getVFS().exists(P)) {
2024
0
        llvm::sys::path::remove_filename(P);
2025
0
        llvm::sys::path::append(P, "libstdc++.6.dylib");
2026
0
        if (getVFS().exists(P)) {
2027
0
          CmdArgs.push_back(Args.MakeArgString(P));
2028
0
          return;
2029
0
        }
2030
2
      }
2031
0
    }
2032
2
2033
2
    // Otherwise, look in the root.
2034
2
    // FIXME: This should be removed someday when we don't have to care about
2035
2
    // 10.6 and earlier, where /usr/lib/libstdc++.dylib does not exist.
2036
2
    if (!getVFS().exists("/usr/lib/libstdc++.dylib") &&
2037
2
        
getVFS().exists("/usr/lib/libstdc++.6.dylib")0
) {
2038
0
      CmdArgs.push_back("/usr/lib/libstdc++.6.dylib");
2039
0
      return;
2040
0
    }
2041
2
2042
2
    // Otherwise, let the linker search.
2043
2
    CmdArgs.push_back("-lstdc++");
2044
2
    break;
2045
1.78k
  }
2046
1.78k
}
2047
2048
void DarwinClang::AddCCKextLibArgs(const ArgList &Args,
2049
2
                                   ArgStringList &CmdArgs) const {
2050
2
  // For Darwin platforms, use the compiler-rt-based support library
2051
2
  // instead of the gcc-provided one (which is also incidentally
2052
2
  // only present in the gcc lib dir, which makes it hard to find).
2053
2
2054
2
  SmallString<128> P(getDriver().ResourceDir);
2055
2
  llvm::sys::path::append(P, "lib", "darwin");
2056
2
2057
2
  // Use the newer cc_kext for iOS ARM after 6.0.
2058
2
  if (isTargetWatchOS()) {
2059
1
    llvm::sys::path::append(P, "libclang_rt.cc_kext_watchos.a");
2060
1
  } else if (isTargetTvOS()) {
2061
1
    llvm::sys::path::append(P, "libclang_rt.cc_kext_tvos.a");
2062
1
  } else 
if (0
isTargetIPhoneOS()0
) {
2063
0
    llvm::sys::path::append(P, "libclang_rt.cc_kext_ios.a");
2064
0
  } else {
2065
0
    llvm::sys::path::append(P, "libclang_rt.cc_kext.a");
2066
0
  }
2067
2
2068
2
  // For now, allow missing resource libraries to support developers who may
2069
2
  // not have compiler-rt checked out or integrated into their build.
2070
2
  if (getVFS().exists(P))
2071
2
    CmdArgs.push_back(Args.MakeArgString(P));
2072
2
}
2073
2074
DerivedArgList *MachO::TranslateArgs(const DerivedArgList &Args,
2075
                                     StringRef BoundArch,
2076
23.9k
                                     Action::OffloadKind) const {
2077
23.9k
  DerivedArgList *DAL = new DerivedArgList(Args.getBaseArgs());
2078
23.9k
  const OptTable &Opts = getDriver().getOpts();
2079
23.9k
2080
23.9k
  // FIXME: We really want to get out of the tool chain level argument
2081
23.9k
  // translation business, as it makes the driver functionality much
2082
23.9k
  // more opaque. For now, we follow gcc closely solely for the
2083
23.9k
  // purpose of easily achieving feature parity & testability. Once we
2084
23.9k
  // have something that works, we should reevaluate each translation
2085
23.9k
  // and try to push it down into tool specific logic.
2086
23.9k
2087
716k
  for (Arg *A : Args) {
2088
716k
    if (A->getOption().matches(options::OPT_Xarch__)) {
2089
13
      // Skip this argument unless the architecture matches either the toolchain
2090
13
      // triple arch, or the arch being bound.
2091
13
      llvm::Triple::ArchType XarchArch =
2092
13
          tools::darwin::getArchTypeForMachOArchName(A->getValue(0));
2093
13
      if (!(XarchArch == getArch() ||
2094
13
            
(4
!BoundArch.empty()4
&&
2095
4
             XarchArch ==
2096
3
                 tools::darwin::getArchTypeForMachOArchName(BoundArch))))
2097
4
        continue;
2098
9
2099
9
      Arg *OriginalArg = A;
2100
9
      unsigned Index = Args.getBaseArgs().MakeIndex(A->getValue(1));
2101
9
      unsigned Prev = Index;
2102
9
      std::unique_ptr<Arg> XarchArg(Opts.ParseOneArg(Args, Index));
2103
9
2104
9
      // If the argument parsing failed or more than one argument was
2105
9
      // consumed, the -Xarch_ argument's parameter tried to consume
2106
9
      // extra arguments. Emit an error and ignore.
2107
9
      //
2108
9
      // We also want to disallow any options which would alter the
2109
9
      // driver behavior; that isn't going to work in our model. We
2110
9
      // use isDriverOption() as an approximation, although things
2111
9
      // like -O4 are going to slip through.
2112
9
      if (!XarchArg || 
Index > Prev + 17
) {
2113
2
        getDriver().Diag(diag::err_drv_invalid_Xarch_argument_with_args)
2114
2
            << A->getAsString(Args);
2115
2
        continue;
2116
7
      } else if (XarchArg->getOption().hasFlag(options::DriverOption)) {
2117
1
        getDriver().Diag(diag::err_drv_invalid_Xarch_argument_isdriver)
2118
1
            << A->getAsString(Args);
2119
1
        continue;
2120
1
      }
2121
6
2122
6
      XarchArg->setBaseArg(A);
2123
6
2124
6
      A = XarchArg.release();
2125
6
      DAL->AddSynthesizedArg(A);
2126
6
2127
6
      // Linker input arguments require custom handling. The problem is that we
2128
6
      // have already constructed the phase actions, so we can not treat them as
2129
6
      // "input arguments".
2130
6
      if (A->getOption().hasFlag(options::LinkerInput)) {
2131
2
        // Convert the argument into individual Zlinker_input_args.
2132
2
        for (const char *Value : A->getValues()) {
2133
2
          DAL->AddSeparateArg(
2134
2
              OriginalArg, Opts.getOption(options::OPT_Zlinker_input), Value);
2135
2
        }
2136
2
        continue;
2137
2
      }
2138
716k
    }
2139
716k
2140
716k
    // Sob. These is strictly gcc compatible for the time being. Apple
2141
716k
    // gcc translates options twice, which means that self-expanding
2142
716k
    // options add duplicates.
2143
716k
    switch ((options::ID)A->getOption().getID()) {
2144
716k
    default:
2145
715k
      DAL->append(A);
2146
715k
      break;
2147
716k
2148
716k
    case options::OPT_mkernel:
2149
1.04k
    case options::OPT_fapple_kext:
2150
1.04k
      DAL->append(A);
2151
1.04k
      DAL->AddFlagArg(A, Opts.getOption(options::OPT_static));
2152
1.04k
      break;
2153
1.04k
2154
1.04k
    case options::OPT_dependency_file:
2155
0
      DAL->AddSeparateArg(A, Opts.getOption(options::OPT_MF), A->getValue());
2156
0
      break;
2157
1.04k
2158
1.04k
    case options::OPT_gfull:
2159
0
      DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
2160
0
      DAL->AddFlagArg(
2161
0
          A, Opts.getOption(options::OPT_fno_eliminate_unused_debug_symbols));
2162
0
      break;
2163
1.04k
2164
1.04k
    case options::OPT_gused:
2165
0
      DAL->AddFlagArg(A, Opts.getOption(options::OPT_g_Flag));
2166
0
      DAL->AddFlagArg(
2167
0
          A, Opts.getOption(options::OPT_feliminate_unused_debug_symbols));
2168
0
      break;
2169
1.04k
2170
1.04k
    case options::OPT_shared:
2171
4
      DAL->AddFlagArg(A, Opts.getOption(options::OPT_dynamiclib));
2172
4
      break;
2173
1.04k
2174
1.04k
    case options::OPT_fconstant_cfstrings:
2175
0
      DAL->AddFlagArg(A, Opts.getOption(options::OPT_mconstant_cfstrings));
2176
0
      break;
2177
1.04k
2178
1.04k
    case options::OPT_fno_constant_cfstrings:
2179
0
      DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_constant_cfstrings));
2180
0
      break;
2181
1.04k
2182
1.04k
    case options::OPT_Wnonportable_cfstrings:
2183
0
      DAL->AddFlagArg(A,
2184
0
                      Opts.getOption(options::OPT_mwarn_nonportable_cfstrings));
2185
0
      break;
2186
1.04k
2187
1.04k
    case options::OPT_Wno_nonportable_cfstrings:
2188
0
      DAL->AddFlagArg(
2189
0
          A, Opts.getOption(options::OPT_mno_warn_nonportable_cfstrings));
2190
0
      break;
2191
1.04k
2192
1.04k
    case options::OPT_fpascal_strings:
2193
5
      DAL->AddFlagArg(A, Opts.getOption(options::OPT_mpascal_strings));
2194
5
      break;
2195
1.04k
2196
1.04k
    case options::OPT_fno_pascal_strings:
2197
4
      DAL->AddFlagArg(A, Opts.getOption(options::OPT_mno_pascal_strings));
2198
4
      break;
2199
716k
    }
2200
716k
  }
2201
23.9k
2202
23.9k
  if (getTriple().getArch() == llvm::Triple::x86 ||
2203
23.9k
      
getTriple().getArch() == llvm::Triple::x86_6423.2k
)
2204
11.3k
    if (!Args.hasArgNoClaim(options::OPT_mtune_EQ))
2205
11.3k
      DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_mtune_EQ),
2206
11.3k
                        "core2");
2207
23.9k
2208
23.9k
  // Add the arch options based on the particular spelling of -arch, to match
2209
23.9k
  // how the driver driver works.
2210
23.9k
  if (!BoundArch.empty()) {
2211
22.8k
    StringRef Name = BoundArch;
2212
22.8k
    const Option MCpu = Opts.getOption(options::OPT_mcpu_EQ);
2213
22.8k
    const Option MArch = Opts.getOption(clang::driver::options::OPT_march_EQ);
2214
22.8k
2215
22.8k
    // This code must be kept in sync with LLVM's getArchTypeForDarwinArch,
2216
22.8k
    // which defines the list of which architectures we accept.
2217
22.8k
    if (Name == "ppc")
2218
6
      ;
2219
22.8k
    else if (Name == "ppc601")
2220
0
      DAL->AddJoinedArg(nullptr, MCpu, "601");
2221
22.8k
    else if (Name == "ppc603")
2222
0
      DAL->AddJoinedArg(nullptr, MCpu, "603");
2223
22.8k
    else if (Name == "ppc604")
2224
0
      DAL->AddJoinedArg(nullptr, MCpu, "604");
2225
22.8k
    else if (Name == "ppc604e")
2226
0
      DAL->AddJoinedArg(nullptr, MCpu, "604e");
2227
22.8k
    else if (Name == "ppc750")
2228
0
      DAL->AddJoinedArg(nullptr, MCpu, "750");
2229
22.8k
    else if (Name == "ppc7400")
2230
0
      DAL->AddJoinedArg(nullptr, MCpu, "7400");
2231
22.8k
    else if (Name == "ppc7450")
2232
0
      DAL->AddJoinedArg(nullptr, MCpu, "7450");
2233
22.8k
    else if (Name == "ppc970")
2234
0
      DAL->AddJoinedArg(nullptr, MCpu, "970");
2235
22.8k
2236
22.8k
    else if (Name == "ppc64" || 
Name == "ppc64le"22.8k
)
2237
3
      DAL->AddFlagArg(nullptr, Opts.getOption(options::OPT_m64));
2238
22.8k
2239
22.8k
    else if (Name == "i386")
2240
642
      ;
2241
22.2k
    else if (Name == "i486")
2242
0
      DAL->AddJoinedArg(nullptr, MArch, "i486");
2243
22.2k
    else if (Name == "i586")
2244
0
      DAL->AddJoinedArg(nullptr, MArch, "i586");
2245
22.2k
    else if (Name == "i686")
2246
12
      DAL->AddJoinedArg(nullptr, MArch, "i686");
2247
22.2k
    else if (Name == "pentium")
2248
0
      DAL->AddJoinedArg(nullptr, MArch, "pentium");
2249
22.2k
    else if (Name == "pentium2")
2250
0
      DAL->AddJoinedArg(nullptr, MArch, "pentium2");
2251
22.2k
    else if (Name == "pentpro")
2252
0
      DAL->AddJoinedArg(nullptr, MArch, "pentiumpro");
2253
22.2k
    else if (Name == "pentIIm3")
2254
0
      DAL->AddJoinedArg(nullptr, MArch, "pentium2");
2255
22.2k
2256
22.2k
    else if (Name == "x86_64" || 
Name == "x86_64h"13.0k
)
2257
9.90k
      DAL->AddFlagArg(nullptr, Opts.getOption(options::OPT_m64));
2258
12.3k
2259
12.3k
    else if (Name == "arm")
2260
5
      DAL->AddJoinedArg(nullptr, MArch, "armv4t");
2261
12.3k
    else if (Name == "armv4t")
2262
0
      DAL->AddJoinedArg(nullptr, MArch, "armv4t");
2263
12.3k
    else if (Name == "armv5")
2264
1
      DAL->AddJoinedArg(nullptr, MArch, "armv5tej");
2265
12.3k
    else if (Name == "xscale")
2266
0
      DAL->AddJoinedArg(nullptr, MArch, "xscale");
2267
12.3k
    else if (Name == "armv6")
2268
15
      DAL->AddJoinedArg(nullptr, MArch, "armv6k");
2269
12.3k
    else if (Name == "armv6m")
2270
182
      DAL->AddJoinedArg(nullptr, MArch, "armv6m");
2271
12.1k
    else if (Name == "armv7")
2272
973
      DAL->AddJoinedArg(nullptr, MArch, "armv7a");
2273
11.1k
    else if (Name == "armv7em")
2274
412
      DAL->AddJoinedArg(nullptr, MArch, "armv7em");
2275
10.7k
    else if (Name == "armv7k")
2276
699
      DAL->AddJoinedArg(nullptr, MArch, "armv7k");
2277
10.0k
    else if (Name == "armv7m")
2278
213
      DAL->AddJoinedArg(nullptr, MArch, "armv7m");
2279
9.83k
    else if (Name == "armv7s")
2280
474
      DAL->AddJoinedArg(nullptr, MArch, "armv7s");
2281
22.8k
  }
2282
23.9k
2283
23.9k
  return DAL;
2284
23.9k
}
2285
2286
void MachO::AddLinkRuntimeLibArgs(const ArgList &Args,
2287
                                  ArgStringList &CmdArgs,
2288
13
                                  bool ForceLinkBuiltinRT) const {
2289
13
  // Embedded targets are simple at the moment, not supporting sanitizers and
2290
13
  // with different libraries for each member of the product { static, PIC } x
2291
13
  // { hard-float, soft-float }
2292
13
  llvm::SmallString<32> CompilerRT = StringRef("");
2293
13
  CompilerRT +=
2294
13
      (tools::arm::getARMFloatABI(*this, Args) == tools::arm::FloatABI::Hard)
2295
13
          ? 
"hard"4
2296
13
          : 
"soft"9
;
2297
13
  CompilerRT += Args.hasArg(options::OPT_fPIC) ? 
"_pic"4
:
"_static"9
;
2298
13
2299
13
  AddLinkRuntimeLib(Args, CmdArgs, CompilerRT, RLO_IsEmbedded);
2300
13
}
2301
2302
20.1k
bool Darwin::isAlignedAllocationUnavailable() const {
2303
20.1k
  llvm::Triple::OSType OS;
2304
20.1k
2305
20.1k
  switch (TargetPlatform) {
2306
20.1k
  case MacOS: // Earlier than 10.13.
2307
10.2k
    OS = llvm::Triple::MacOSX;
2308
10.2k
    break;
2309
20.1k
  case IPhoneOS:
2310
9.87k
    OS = llvm::Triple::IOS;
2311
9.87k
    break;
2312
20.1k
  case TvOS: // Earlier than 11.0.
2313
32
    OS = llvm::Triple::TvOS;
2314
32
    break;
2315
20.1k
  case WatchOS: // Earlier than 4.0.
2316
38
    OS = llvm::Triple::WatchOS;
2317
38
    break;
2318
20.1k
  }
2319
20.1k
2320
20.1k
  return TargetVersion < alignedAllocMinVersion(OS);
2321
20.1k
}
2322
2323
void Darwin::addClangTargetOptions(const llvm::opt::ArgList &DriverArgs,
2324
                                   llvm::opt::ArgStringList &CC1Args,
2325
20.1k
                                   Action::OffloadKind DeviceOffloadKind) const {
2326
20.1k
  // Pass "-faligned-alloc-unavailable" only when the user hasn't manually
2327
20.1k
  // enabled or disabled aligned allocations.
2328
20.1k
  if (!DriverArgs.hasArgNoClaim(options::OPT_faligned_allocation,
2329
20.1k
                                options::OPT_fno_aligned_allocation) &&
2330
20.1k
      
isAlignedAllocationUnavailable()20.1k
)
2331
4.85k
    CC1Args.push_back("-faligned-alloc-unavailable");
2332
20.1k
2333
20.1k
  if (SDKInfo) {
2334
11.3k
    /// Pass the SDK version to the compiler when the SDK information is
2335
11.3k
    /// available.
2336
11.3k
    std::string Arg;
2337
11.3k
    llvm::raw_string_ostream OS(Arg);
2338
11.3k
    OS << "-target-sdk-version=" << SDKInfo->getVersion();
2339
11.3k
    CC1Args.push_back(DriverArgs.MakeArgString(OS.str()));
2340
11.3k
  }
2341
20.1k
}
2342
2343
DerivedArgList *
2344
Darwin::TranslateArgs(const DerivedArgList &Args, StringRef BoundArch,
2345
23.0k
                      Action::OffloadKind DeviceOffloadKind) const {
2346
23.0k
  // First get the generic Apple args, before moving onto Darwin-specific ones.
2347
23.0k
  DerivedArgList *DAL =
2348
23.0k
      MachO::TranslateArgs(Args, BoundArch, DeviceOffloadKind);
2349
23.0k
  const OptTable &Opts = getDriver().getOpts();
2350
23.0k
2351
23.0k
  // If no architecture is bound, none of the translations here are relevant.
2352
23.0k
  if (BoundArch.empty())
2353
1.00k
    return DAL;
2354
22.0k
2355
22.0k
  // Add an explicit version min argument for the deployment target. We do this
2356
22.0k
  // after argument translation because -Xarch_ arguments may add a version min
2357
22.0k
  // argument.
2358
22.0k
  AddDeploymentTarget(*DAL);
2359
22.0k
2360
22.0k
  // For iOS 6, undo the translation to add -static for -mkernel/-fapple-kext.
2361
22.0k
  // FIXME: It would be far better to avoid inserting those -static arguments,
2362
22.0k
  // but we can't check the deployment target in the translation code until
2363
22.0k
  // it is set here.
2364
22.0k
  if (isTargetWatchOSBased() ||
2365
22.0k
      
(22.0k
isTargetIOSBased()22.0k
&&
!isIPhoneOSVersionLT(6, 0)11.9k
)) {
2366
290k
    for (ArgList::iterator it = DAL->begin(), ie = DAL->end(); it != ie; ) {
2367
278k
      Arg *A = *it;
2368
278k
      ++it;
2369
278k
      if (A->getOption().getID() != options::OPT_mkernel &&
2370
278k
          
A->getOption().getID() != options::OPT_fapple_kext277k
)
2371
277k
        continue;
2372
701
      assert(it != ie && "unexpected argument translation");
2373
701
      A = *it;
2374
701
      assert(A->getOption().getID() == options::OPT_static &&
2375
701
             "missing expected -static argument");
2376
701
      *it = nullptr;
2377
701
      ++it;
2378
701
    }
2379
11.8k
  }
2380
22.0k
2381
22.0k
  if (!Args.getLastArg(options::OPT_stdlib_EQ) &&
2382
22.0k
      
GetCXXStdlibType(Args) == ToolChain::CST_Libcxx19.6k
)
2383
17.2k
    DAL->AddJoinedArg(nullptr, Opts.getOption(options::OPT_stdlib_EQ),
2384
17.2k
                      "libc++");
2385
22.0k
2386
22.0k
  // Validate the C++ standard library choice.
2387
22.0k
  CXXStdlibType Type = GetCXXStdlibType(*DAL);
2388
22.0k
  if (Type == ToolChain::CST_Libcxx) {
2389
19.6k
    // Check whether the target provides libc++.
2390
19.6k
    StringRef where;
2391
19.6k
2392
19.6k
    // Complain about targeting iOS < 5.0 in any way.
2393
19.6k
    if (isTargetIOSBased() && 
isIPhoneOSVersionLT(5, 0)10.4k
)
2394
0
      where = "iOS 5.0";
2395
19.6k
2396
19.6k
    if (where != StringRef()) {
2397
0
      getDriver().Diag(clang::diag::err_drv_invalid_libcxx_deployment) << where;
2398
0
    }
2399
19.6k
  }
2400
22.0k
2401
22.0k
  auto Arch = tools::darwin::getArchTypeForMachOArchName(BoundArch);
2402
22.0k
  if ((Arch == llvm::Triple::arm || 
Arch == llvm::Triple::thumb19.9k
)) {
2403
2.16k
    if (Args.hasFlag(options::OPT_fomit_frame_pointer,
2404
2.16k
                     options::OPT_fno_omit_frame_pointer, false))
2405
2
      getDriver().Diag(clang::diag::warn_drv_unsupported_opt_for_target)
2406
2
          << "-fomit-frame-pointer" << BoundArch;
2407
2.16k
  }
2408
22.0k
2409
22.0k
  return DAL;
2410
22.0k
}
2411
2412
20.8k
bool MachO::IsUnwindTablesDefault(const ArgList &Args) const {
2413
20.8k
  // Unwind tables are not emitted if -fno-exceptions is supplied (except when
2414
20.8k
  // targeting x86_64).
2415
20.8k
  return getArch() == llvm::Triple::x86_64 ||
2416
20.8k
         
(10.9k
GetExceptionModel(Args) != llvm::ExceptionHandling::SjLj10.9k
&&
2417
10.9k
          Args.hasFlag(options::OPT_fexceptions, options::OPT_fno_exceptions,
2418
9.49k
                       true));
2419
20.8k
}
2420
2421
21.6k
bool MachO::UseDwarfDebugFlags() const {
2422
21.6k
  if (const char *S = ::getenv("RC_DEBUG_OPTIONS"))
2423
21
    return S[0] != '\0';
2424
21.6k
  return false;
2425
21.6k
}
2426
2427
30.1k
llvm::ExceptionHandling Darwin::GetExceptionModel(const ArgList &Args) const {
2428
30.1k
  // Darwin uses SjLj exceptions on ARM.
2429
30.1k
  if (getTriple().getArch() != llvm::Triple::arm &&
2430
30.1k
      
getTriple().getArch() != llvm::Triple::thumb25.9k
)
2431
25.9k
    return llvm::ExceptionHandling::None;
2432
4.25k
2433
4.25k
  // Only watchOS uses the new DWARF/Compact unwinding method.
2434
4.25k
  llvm::Triple Triple(ComputeLLVMTriple(Args));
2435
4.25k
  if (Triple.isWatchABI())
2436
1.38k
    return llvm::ExceptionHandling::DwarfCFI;
2437
2.87k
2438
2.87k
  return llvm::ExceptionHandling::SjLj;
2439
2.87k
}
2440
2441
7
bool Darwin::SupportsEmbeddedBitcode() const {
2442
7
  assert(TargetInitialized && "Target not initialized!");
2443
7
  if (isTargetIPhoneOS() && isIPhoneOSVersionLT(6, 0))
2444
1
    return false;
2445
6
  return true;
2446
6
}
2447
2448
41.5k
bool MachO::isPICDefault() const { return true; }
2449
2450
21.6k
bool MachO::isPIEDefault() const { return false; }
2451
2452
21.6k
bool MachO::isPICDefaultForced() const {
2453
21.6k
  return (getArch() == llvm::Triple::x86_64 ||
2454
21.6k
          
getArch() == llvm::Triple::aarch6411.6k
);
2455
21.6k
}
2456
2457
41.7k
bool MachO::SupportsProfiling() const {
2458
41.7k
  // Profiling instrumentation is only supported on x86.
2459
41.7k
  return getArch() == llvm::Triple::x86 || 
getArch() == llvm::Triple::x86_6440.4k
;
2460
41.7k
}
2461
2462
void Darwin::addMinVersionArgs(const ArgList &Args,
2463
2.61k
                               ArgStringList &CmdArgs) const {
2464
2.61k
  VersionTuple TargetVersion = getTargetVersion();
2465
2.61k
2466
2.61k
  if (isTargetWatchOS())
2467
12
    CmdArgs.push_back("-watchos_version_min");
2468
2.60k
  else if (isTargetWatchOSSimulator())
2469
3
    CmdArgs.push_back("-watchos_simulator_version_min");
2470
2.60k
  else if (isTargetTvOS())
2471
7
    CmdArgs.push_back("-tvos_version_min");
2472
2.59k
  else if (isTargetTvOSSimulator())
2473
7
    CmdArgs.push_back("-tvos_simulator_version_min");
2474
2.58k
  else if (isTargetIOSSimulator())
2475
27
    CmdArgs.push_back("-ios_simulator_version_min");
2476
2.56k
  else if (isTargetIOSBased())
2477
2.03k
    CmdArgs.push_back("-iphoneos_version_min");
2478
523
  else {
2479
523
    assert(isTargetMacOS() && "unexpected target");
2480
523
    CmdArgs.push_back("-macosx_version_min");
2481
523
  }
2482
2.61k
2483
2.61k
  CmdArgs.push_back(Args.MakeArgString(TargetVersion.getAsString()));
2484
2.61k
}
2485
2486
void Darwin::addStartObjectFileArgs(const ArgList &Args,
2487
2.60k
                                    ArgStringList &CmdArgs) const {
2488
2.60k
  // Derived from startfile spec.
2489
2.60k
  if (Args.hasArg(options::OPT_dynamiclib)) {
2490
48
    // Derived from darwin_dylib1 spec.
2491
48
    if (isTargetWatchOSBased()) {
2492
0
      ; // watchOS does not need dylib1.o.
2493
48
    } else if (isTargetIOSSimulator()) {
2494
12
      ; // iOS simulator does not need dylib1.o.
2495
36
    } else if (isTargetIPhoneOS()) {
2496
20
      if (isIPhoneOSVersionLT(3, 1))
2497
1
        CmdArgs.push_back("-ldylib1.o");
2498
20
    } else {
2499
16
      if (isMacosxVersionLT(10, 5))
2500
0
        CmdArgs.push_back("-ldylib1.o");
2501
16
      else if (isMacosxVersionLT(10, 6))
2502
0
        CmdArgs.push_back("-ldylib1.10.5.o");
2503
16
    }
2504
2.56k
  } else {
2505
2.56k
    if (Args.hasArg(options::OPT_bundle)) {
2506
3
      if (!Args.hasArg(options::OPT_static)) {
2507
3
        // Derived from darwin_bundle1 spec.
2508
3
        if (isTargetWatchOSBased()) {
2509
0
          ; // watchOS does not need bundle1.o.
2510
3
        } else if (isTargetIOSSimulator()) {
2511
1
          ; // iOS simulator does not need bundle1.o.
2512
2
        } else if (isTargetIPhoneOS()) {
2513
2
          if (isIPhoneOSVersionLT(3, 1))
2514
1
            CmdArgs.push_back("-lbundle1.o");
2515
2
        } else {
2516
0
          if (isMacosxVersionLT(10, 6))
2517
0
            CmdArgs.push_back("-lbundle1.o");
2518
0
        }
2519
3
      }
2520
2.55k
    } else {
2521
2.55k
      if (Args.hasArg(options::OPT_pg) && 
SupportsProfiling()3
) {
2522
3
        if (isTargetMacOS() && 
isMacosxVersionLT(10, 9)2
) {
2523
1
          if (Args.hasArg(options::OPT_static) ||
2524
1
              Args.hasArg(options::OPT_object) ||
2525
1
              Args.hasArg(options::OPT_preload)) {
2526
0
            CmdArgs.push_back("-lgcrt0.o");
2527
1
          } else {
2528
1
            CmdArgs.push_back("-lgcrt1.o");
2529
1
2530
1
            // darwin_crt2 spec is empty.
2531
1
          }
2532
1
          // By default on OS X 10.8 and later, we don't link with a crt1.o
2533
1
          // file and the linker knows to use _main as the entry point.  But,
2534
1
          // when compiling with -pg, we need to link with the gcrt1.o file,
2535
1
          // so pass the -no_new_main option to tell the linker to use the
2536
1
          // "start" symbol as the entry point.
2537
1
          if (isTargetMacOS() && !isMacosxVersionLT(10, 8))
2538
1
            CmdArgs.push_back("-no_new_main");
2539
2
        } else {
2540
2
          getDriver().Diag(diag::err_drv_clang_unsupported_opt_pg_darwin)
2541
2
              << isTargetMacOS();
2542
2
        }
2543
2.55k
      } else {
2544
2.55k
        if (Args.hasArg(options::OPT_static) ||
2545
2.55k
            
Args.hasArg(options::OPT_object)2.55k
||
2546
2.55k
            
Args.hasArg(options::OPT_preload)2.55k
) {
2547
3
          CmdArgs.push_back("-lcrt0.o");
2548
2.55k
        } else {
2549
2.55k
          // Derived from darwin_crt1 spec.
2550
2.55k
          if (isTargetWatchOSBased()) {
2551
15
            ; // watchOS does not need crt1.o.
2552
2.53k
          } else if (isTargetIOSSimulator()) {
2553
20
            ; // iOS simulator does not need crt1.o.
2554
2.51k
          } else if (isTargetIPhoneOS()) {
2555
2.01k
            if (getArch() == llvm::Triple::aarch64)
2556
1.97k
              ; // iOS does not need any crt1 files for arm64
2557
44
            else if (isIPhoneOSVersionLT(3, 1))
2558
2
              CmdArgs.push_back("-lcrt1.o");
2559
42
            else if (isIPhoneOSVersionLT(6, 0))
2560
19
              CmdArgs.push_back("-lcrt1.3.1.o");
2561
2.01k
          } else {
2562
498
            if (isMacosxVersionLT(10, 5))
2563
3
              CmdArgs.push_back("-lcrt1.o");
2564
495
            else if (isMacosxVersionLT(10, 6))
2565
11
              CmdArgs.push_back("-lcrt1.10.5.o");
2566
484
            else if (isMacosxVersionLT(10, 8))
2567
48
              CmdArgs.push_back("-lcrt1.10.6.o");
2568
498
2569
498
            // darwin_crt2 spec is empty.
2570
498
          }
2571
2.55k
        }
2572
2.55k
      }
2573
2.55k
    }
2574
2.56k
  }
2575
2.60k
2576
2.60k
  if (!isTargetIPhoneOS() && 
Args.hasArg(options::OPT_shared_libgcc)566
&&
2577
2.60k
      
!isTargetWatchOS()0
&&
isMacosxVersionLT(10, 5)0
) {
2578
0
    const char *Str = Args.MakeArgString(GetFilePath("crt3.o"));
2579
0
    CmdArgs.push_back(Str);
2580
0
  }
2581
2.60k
}
2582
2583
29
void Darwin::CheckObjCARC() const {
2584
29
  if (isTargetIOSBased() || 
isTargetWatchOSBased()26
||
2585
29
      
(25
isTargetMacOS()25
&&
!isMacosxVersionLT(10, 6)25
))
2586
26
    return;
2587
3
  getDriver().Diag(diag::err_arc_unsupported_on_toolchain);
2588
3
}
2589
2590
21.5k
SanitizerMask Darwin::getSupportedSanitizers() const {
2591
21.5k
  const bool IsX86_64 = getTriple().getArch() == llvm::Triple::x86_64;
2592
21.5k
  SanitizerMask Res = ToolChain::getSupportedSanitizers();
2593
21.5k
  Res |= SanitizerKind::Address;
2594
21.5k
  Res |= SanitizerKind::PointerCompare;
2595
21.5k
  Res |= SanitizerKind::PointerSubtract;
2596
21.5k
  Res |= SanitizerKind::Leak;
2597
21.5k
  Res |= SanitizerKind::Fuzzer;
2598
21.5k
  Res |= SanitizerKind::FuzzerNoLink;
2599
21.5k
  Res |= SanitizerKind::Function;
2600
21.5k
2601
21.5k
  // Prior to 10.9, macOS shipped a version of the C++ standard library without
2602
21.5k
  // C++11 support. The same is true of iOS prior to version 5. These OS'es are
2603
21.5k
  // incompatible with -fsanitize=vptr.
2604
21.5k
  if (!(isTargetMacOS() && 
isMacosxVersionLT(10, 9)10.0k
)
2605
21.5k
      && 
!(20.6k
isTargetIPhoneOS()20.6k
&&
isIPhoneOSVersionLT(5, 0)10.5k
))
2606
20.6k
    Res |= SanitizerKind::Vptr;
2607
21.5k
2608
21.5k
  if (isTargetMacOS()) {
2609
10.0k
    if (IsX86_64)
2610
9.32k
      Res |= SanitizerKind::Thread;
2611
11.4k
  } else if (isTargetIOSSimulator() || 
isTargetTvOSSimulator()10.5k
) {
2612
913
    if (IsX86_64)
2613
541
      Res |= SanitizerKind::Thread;
2614
913
  }
2615
21.5k
  return Res;
2616
21.5k
}
2617
2618
92
void Darwin::printVerboseInfo(raw_ostream &OS) const {
2619
92
  CudaInstallation.print(OS);
2620
92
}