1 //===-- PlatformAppleSimulator.cpp ----------------------------------------===//
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 "PlatformAppleSimulator.h"
10
11 #if defined(__APPLE__)
12 #include <dlfcn.h>
13 #endif
14
15 #include "lldb/Core/Debugger.h"
16 #include "lldb/Core/Module.h"
17 #include "lldb/Core/PluginManager.h"
18 #include "lldb/Host/HostInfo.h"
19 #include "lldb/Host/PseudoTerminal.h"
20 #include "lldb/Target/Process.h"
21 #include "lldb/Utility/LLDBAssert.h"
22 #include "lldb/Utility/LLDBLog.h"
23 #include "lldb/Utility/Log.h"
24 #include "lldb/Utility/Status.h"
25 #include "lldb/Utility/StreamString.h"
26
27 #include "llvm/Support/Threading.h"
28
29 #include <mutex>
30 #include <thread>
31
32 using namespace lldb;
33 using namespace lldb_private;
34
35 #if !defined(__APPLE__)
36 #define UNSUPPORTED_ERROR ("Apple simulators aren't supported on this platform")
37 #endif
38
39 /// Default Constructor
PlatformAppleSimulator(const char * class_name,const char * description,ConstString plugin_name,llvm::Triple::OSType preferred_os,llvm::SmallVector<llvm::StringRef,4> supported_triples,llvm::StringRef sdk,lldb_private::XcodeSDK::Type sdk_type,CoreSimulatorSupport::DeviceType::ProductFamilyID kind)40 PlatformAppleSimulator::PlatformAppleSimulator(
41 const char *class_name, const char *description, ConstString plugin_name,
42 llvm::Triple::OSType preferred_os,
43 llvm::SmallVector<llvm::StringRef, 4> supported_triples,
44 llvm::StringRef sdk, lldb_private::XcodeSDK::Type sdk_type,
45 CoreSimulatorSupport::DeviceType::ProductFamilyID kind)
46 : PlatformDarwin(true), m_class_name(class_name),
47 m_description(description), m_plugin_name(plugin_name), m_kind(kind),
48 m_os_type(preferred_os), m_supported_triples(supported_triples),
49 m_sdk(sdk), m_sdk_type(sdk_type) {}
50
51 /// Destructor.
52 ///
53 /// The destructor is virtual since this class is designed to be
54 /// inherited from by the plug-in instance.
55 PlatformAppleSimulator::~PlatformAppleSimulator() = default;
56
LaunchProcess(lldb_private::ProcessLaunchInfo & launch_info)57 lldb_private::Status PlatformAppleSimulator::LaunchProcess(
58 lldb_private::ProcessLaunchInfo &launch_info) {
59 #if defined(__APPLE__)
60 LoadCoreSimulator();
61 CoreSimulatorSupport::Device device(GetSimulatorDevice());
62
63 if (device.GetState() != CoreSimulatorSupport::Device::State::Booted) {
64 Status boot_err;
65 device.Boot(boot_err);
66 if (boot_err.Fail())
67 return boot_err;
68 }
69
70 auto spawned = device.Spawn(launch_info);
71
72 if (spawned) {
73 launch_info.SetProcessID(spawned.GetPID());
74 return Status();
75 } else
76 return spawned.GetError();
77 #else
78 Status err;
79 err.SetErrorString(UNSUPPORTED_ERROR);
80 return err;
81 #endif
82 }
83
GetStatus(Stream & strm)84 void PlatformAppleSimulator::GetStatus(Stream &strm) {
85 Platform::GetStatus(strm);
86 if (!m_sdk.empty())
87 strm << " SDK Path: \"" << m_sdk << "\"\n";
88 else
89 strm << " SDK Path: error: unable to locate SDK\n";
90
91 #if defined(__APPLE__)
92 // This will get called by subclasses, so just output status on the current
93 // simulator
94 PlatformAppleSimulator::LoadCoreSimulator();
95
96 std::string developer_dir = HostInfo::GetXcodeDeveloperDirectory().GetPath();
97 CoreSimulatorSupport::DeviceSet devices =
98 CoreSimulatorSupport::DeviceSet::GetAvailableDevices(
99 developer_dir.c_str());
100 const size_t num_devices = devices.GetNumDevices();
101 if (num_devices) {
102 strm.Printf("Available devices:\n");
103 for (size_t i = 0; i < num_devices; ++i) {
104 CoreSimulatorSupport::Device device = devices.GetDeviceAtIndex(i);
105 strm << " " << device.GetUDID() << ": " << device.GetName() << "\n";
106 }
107
108 if (m_device.has_value() && m_device->operator bool()) {
109 strm << "Current device: " << m_device->GetUDID() << ": "
110 << m_device->GetName();
111 if (m_device->GetState() == CoreSimulatorSupport::Device::State::Booted) {
112 strm << " state = booted";
113 }
114 strm << "\nType \"platform connect <ARG>\" where <ARG> is a device "
115 "UDID or a device name to disconnect and connect to a "
116 "different device.\n";
117
118 } else {
119 strm << "No current device is selected, \"platform connect <ARG>\" "
120 "where <ARG> is a device UDID or a device name to connect to "
121 "a specific device.\n";
122 }
123
124 } else {
125 strm << "No devices are available.\n";
126 }
127 #else
128 strm << UNSUPPORTED_ERROR;
129 #endif
130 }
131
ConnectRemote(Args & args)132 Status PlatformAppleSimulator::ConnectRemote(Args &args) {
133 #if defined(__APPLE__)
134 Status error;
135 if (args.GetArgumentCount() == 1) {
136 if (m_device)
137 DisconnectRemote();
138 PlatformAppleSimulator::LoadCoreSimulator();
139 const char *arg_cstr = args.GetArgumentAtIndex(0);
140 if (arg_cstr) {
141 std::string arg_str(arg_cstr);
142 std::string developer_dir = HostInfo::GetXcodeDeveloperDirectory().GetPath();
143 CoreSimulatorSupport::DeviceSet devices =
144 CoreSimulatorSupport::DeviceSet::GetAvailableDevices(
145 developer_dir.c_str());
146 devices.ForEach(
147 [this, &arg_str](const CoreSimulatorSupport::Device &device) -> bool {
148 if (arg_str == device.GetUDID() || arg_str == device.GetName()) {
149 m_device = device;
150 return false; // Stop iterating
151 } else {
152 return true; // Keep iterating
153 }
154 });
155 if (!m_device)
156 error.SetErrorStringWithFormat(
157 "no device with UDID or name '%s' was found", arg_cstr);
158 }
159 } else {
160 error.SetErrorString("this command take a single UDID argument of the "
161 "device you want to connect to.");
162 }
163 return error;
164 #else
165 Status err;
166 err.SetErrorString(UNSUPPORTED_ERROR);
167 return err;
168 #endif
169 }
170
DisconnectRemote()171 Status PlatformAppleSimulator::DisconnectRemote() {
172 #if defined(__APPLE__)
173 m_device.reset();
174 return Status();
175 #else
176 Status err;
177 err.SetErrorString(UNSUPPORTED_ERROR);
178 return err;
179 #endif
180 }
181
182 lldb::ProcessSP
DebugProcess(ProcessLaunchInfo & launch_info,Debugger & debugger,Target & target,Status & error)183 PlatformAppleSimulator::DebugProcess(ProcessLaunchInfo &launch_info,
184 Debugger &debugger, Target &target,
185 Status &error) {
186 #if defined(__APPLE__)
187 ProcessSP process_sp;
188 // Make sure we stop at the entry point
189 launch_info.GetFlags().Set(eLaunchFlagDebug);
190 // We always launch the process we are going to debug in a separate process
191 // group, since then we can handle ^C interrupts ourselves w/o having to
192 // worry about the target getting them as well.
193 launch_info.SetLaunchInSeparateProcessGroup(true);
194
195 error = LaunchProcess(launch_info);
196 if (error.Success()) {
197 if (launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) {
198 ProcessAttachInfo attach_info(launch_info);
199 process_sp = Attach(attach_info, debugger, &target, error);
200 if (process_sp) {
201 launch_info.SetHijackListener(attach_info.GetHijackListener());
202
203 // Since we attached to the process, it will think it needs to detach
204 // if the process object just goes away without an explicit call to
205 // Process::Kill() or Process::Detach(), so let it know to kill the
206 // process if this happens.
207 process_sp->SetShouldDetach(false);
208
209 // If we didn't have any file actions, the pseudo terminal might have
210 // been used where the secondary side was given as the file to open for
211 // stdin/out/err after we have already opened the primary so we can
212 // read/write stdin/out/err.
213 int pty_fd = launch_info.GetPTY().ReleasePrimaryFileDescriptor();
214 if (pty_fd != PseudoTerminal::invalid_fd) {
215 process_sp->SetSTDIOFileDescriptor(pty_fd);
216 }
217 }
218 }
219 }
220
221 return process_sp;
222 #else
223 return ProcessSP();
224 #endif
225 }
226
GetCoreSimulatorPath()227 FileSpec PlatformAppleSimulator::GetCoreSimulatorPath() {
228 #if defined(__APPLE__)
229 std::lock_guard<std::mutex> guard(m_core_sim_path_mutex);
230 if (!m_core_simulator_framework_path.has_value()) {
231 m_core_simulator_framework_path =
232 FileSpec("/Library/Developer/PrivateFrameworks/CoreSimulator.framework/"
233 "CoreSimulator");
234 FileSystem::Instance().Resolve(*m_core_simulator_framework_path);
235 }
236 return m_core_simulator_framework_path.value();
237 #else
238 return FileSpec();
239 #endif
240 }
241
LoadCoreSimulator()242 void PlatformAppleSimulator::LoadCoreSimulator() {
243 #if defined(__APPLE__)
244 static llvm::once_flag g_load_core_sim_flag;
245 llvm::call_once(g_load_core_sim_flag, [this] {
246 const std::string core_sim_path(GetCoreSimulatorPath().GetPath());
247 if (core_sim_path.size())
248 dlopen(core_sim_path.c_str(), RTLD_LAZY);
249 });
250 #endif
251 }
252
253 #if defined(__APPLE__)
GetSimulatorDevice()254 CoreSimulatorSupport::Device PlatformAppleSimulator::GetSimulatorDevice() {
255 if (!m_device.has_value()) {
256 const CoreSimulatorSupport::DeviceType::ProductFamilyID dev_id = m_kind;
257 std::string developer_dir =
258 HostInfo::GetXcodeDeveloperDirectory().GetPath();
259 m_device = CoreSimulatorSupport::DeviceSet::GetAvailableDevices(
260 developer_dir.c_str())
261 .GetFanciest(dev_id);
262 }
263
264 if (m_device.has_value())
265 return m_device.value();
266 else
267 return CoreSimulatorSupport::Device();
268 }
269 #endif
270
GetSupportedArchitectures(const ArchSpec & process_host_arch)271 std::vector<ArchSpec> PlatformAppleSimulator::GetSupportedArchitectures(
272 const ArchSpec &process_host_arch) {
273 std::vector<ArchSpec> result(m_supported_triples.size());
274 llvm::transform(m_supported_triples, result.begin(),
275 [](llvm::StringRef triple) { return ArchSpec(triple); });
276 return result;
277 }
278
GetXcodeSDKDir(std::string preferred,std::string secondary)279 static llvm::StringRef GetXcodeSDKDir(std::string preferred,
280 std::string secondary) {
281 llvm::StringRef sdk;
282 auto get_sdk = [&](std::string sdk) -> llvm::StringRef {
283 auto sdk_path_or_err = HostInfo::GetXcodeSDKPath(XcodeSDK(std::move(sdk)));
284 if (!sdk_path_or_err) {
285 Debugger::ReportError("Error while searching for Xcode SDK: " +
286 toString(sdk_path_or_err.takeError()));
287 return {};
288 }
289 return *sdk_path_or_err;
290 };
291
292 sdk = get_sdk(preferred);
293 if (sdk.empty())
294 sdk = get_sdk(secondary);
295 return sdk;
296 }
297
CreateInstance(const char * class_name,const char * description,ConstString plugin_name,llvm::SmallVector<llvm::Triple::ArchType,4> supported_arch,llvm::Triple::OSType preferred_os,llvm::SmallVector<llvm::Triple::OSType,4> supported_os,llvm::SmallVector<llvm::StringRef,4> supported_triples,std::string sdk_name_preferred,std::string sdk_name_secondary,lldb_private::XcodeSDK::Type sdk_type,CoreSimulatorSupport::DeviceType::ProductFamilyID kind,bool force,const ArchSpec * arch)298 PlatformSP PlatformAppleSimulator::CreateInstance(
299 const char *class_name, const char *description, ConstString plugin_name,
300 llvm::SmallVector<llvm::Triple::ArchType, 4> supported_arch,
301 llvm::Triple::OSType preferred_os,
302 llvm::SmallVector<llvm::Triple::OSType, 4> supported_os,
303 llvm::SmallVector<llvm::StringRef, 4> supported_triples,
304 std::string sdk_name_preferred, std::string sdk_name_secondary,
305 lldb_private::XcodeSDK::Type sdk_type,
306 CoreSimulatorSupport::DeviceType::ProductFamilyID kind, bool force,
307 const ArchSpec *arch) {
308 Log *log = GetLog(LLDBLog::Platform);
309 if (log) {
310 const char *arch_name;
311 if (arch && arch->GetArchitectureName())
312 arch_name = arch->GetArchitectureName();
313 else
314 arch_name = "<null>";
315
316 const char *triple_cstr =
317 arch ? arch->GetTriple().getTriple().c_str() : "<null>";
318
319 LLDB_LOGF(log, "%s::%s(force=%s, arch={%s,%s})", class_name, __FUNCTION__,
320 force ? "true" : "false", arch_name, triple_cstr);
321 }
322
323 bool create = force;
324 if (!create && arch && arch->IsValid()) {
325 if (std::count(supported_arch.begin(), supported_arch.end(),
326 arch->GetMachine())) {
327 const llvm::Triple &triple = arch->GetTriple();
328 switch (triple.getVendor()) {
329 case llvm::Triple::Apple:
330 create = true;
331 break;
332
333 #if defined(__APPLE__)
334 // Only accept "unknown" for the vendor if the host is Apple and if
335 // "unknown" wasn't specified (it was just returned because it was NOT
336 // specified)
337 case llvm::Triple::UnknownVendor:
338 create = !arch->TripleVendorWasSpecified();
339 break;
340 #endif
341 default:
342 break;
343 }
344
345 if (create) {
346 if (llvm::is_contained(supported_os, triple.getOS()))
347 create = true;
348 #if defined(__APPLE__)
349 // Only accept "unknown" for the OS if the host is Apple and it
350 // "unknown" wasn't specified (it was just returned because it was NOT
351 // specified)
352 else if (triple.getOS() == llvm::Triple::UnknownOS)
353 create = !arch->TripleOSWasSpecified();
354 #endif
355 else
356 create = false;
357 }
358 }
359 }
360 if (create) {
361 LLDB_LOGF(log, "%s::%s() creating platform", class_name, __FUNCTION__);
362
363 llvm::StringRef sdk =
364 GetXcodeSDKDir(sdk_name_preferred, sdk_name_secondary);
365 return PlatformSP(new PlatformAppleSimulator(
366 class_name, description, plugin_name, preferred_os, supported_triples,
367 sdk, sdk_type, kind));
368 }
369
370 LLDB_LOGF(log, "%s::%s() aborting creation of platform", class_name,
371 __FUNCTION__);
372
373 return PlatformSP();
374 }
375
ResolveExecutable(const ModuleSpec & module_spec,lldb::ModuleSP & exe_module_sp,const FileSpecList * module_search_paths_ptr)376 Status PlatformAppleSimulator::ResolveExecutable(
377 const ModuleSpec &module_spec, lldb::ModuleSP &exe_module_sp,
378 const FileSpecList *module_search_paths_ptr) {
379 Status error;
380 // Nothing special to do here, just use the actual file and architecture
381
382 ModuleSpec resolved_module_spec(module_spec);
383
384 // If we have "ls" as the exe_file, resolve the executable loation based on
385 // the current path variables
386 // TODO: resolve bare executables in the Platform SDK
387 // if (!resolved_exe_file.Exists())
388 // resolved_exe_file.ResolveExecutableLocation ();
389
390 // Resolve any executable within a bundle on MacOSX
391 // TODO: verify that this handles shallow bundles, if not then implement one
392 // ourselves
393 Host::ResolveExecutableInBundle(resolved_module_spec.GetFileSpec());
394
395 if (FileSystem::Instance().Exists(resolved_module_spec.GetFileSpec())) {
396 if (resolved_module_spec.GetArchitecture().IsValid()) {
397 error = ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp,
398 NULL, NULL, NULL);
399
400 if (exe_module_sp && exe_module_sp->GetObjectFile())
401 return error;
402 exe_module_sp.reset();
403 }
404 // No valid architecture was specified or the exact ARM slice wasn't found
405 // so ask the platform for the architectures that we should be using (in
406 // the correct order) and see if we can find a match that way
407 StreamString arch_names;
408 llvm::ListSeparator LS;
409 ArchSpec platform_arch;
410 for (const ArchSpec &arch : GetSupportedArchitectures({})) {
411 resolved_module_spec.GetArchitecture() = arch;
412
413 // Only match x86 with x86 and x86_64 with x86_64...
414 if (!module_spec.GetArchitecture().IsValid() ||
415 module_spec.GetArchitecture().GetCore() ==
416 resolved_module_spec.GetArchitecture().GetCore()) {
417 error = ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp,
418 NULL, NULL, NULL);
419 // Did we find an executable using one of the
420 if (error.Success()) {
421 if (exe_module_sp && exe_module_sp->GetObjectFile())
422 break;
423 else
424 error.SetErrorToGenericError();
425 }
426
427 arch_names << LS << platform_arch.GetArchitectureName();
428 }
429 }
430
431 if (error.Fail() || !exe_module_sp) {
432 if (FileSystem::Instance().Readable(resolved_module_spec.GetFileSpec())) {
433 error.SetErrorStringWithFormatv(
434 "'{0}' doesn't contain any '{1}' platform architectures: {2}",
435 resolved_module_spec.GetFileSpec(), GetPluginName(),
436 arch_names.GetString());
437 } else {
438 error.SetErrorStringWithFormat(
439 "'%s' is not readable",
440 resolved_module_spec.GetFileSpec().GetPath().c_str());
441 }
442 }
443 } else {
444 error.SetErrorStringWithFormat("'%s' does not exist",
445 module_spec.GetFileSpec().GetPath().c_str());
446 }
447
448 return error;
449 }
450
GetSymbolFile(const FileSpec & platform_file,const UUID * uuid_ptr,FileSpec & local_file)451 Status PlatformAppleSimulator::GetSymbolFile(const FileSpec &platform_file,
452 const UUID *uuid_ptr,
453 FileSpec &local_file) {
454 Status error;
455 char platform_file_path[PATH_MAX];
456 if (platform_file.GetPath(platform_file_path, sizeof(platform_file_path))) {
457 char resolved_path[PATH_MAX];
458
459 if (!m_sdk.empty()) {
460 ::snprintf(resolved_path, sizeof(resolved_path), "%s/%s",
461 m_sdk.str().c_str(), platform_file_path);
462
463 // First try in the SDK and see if the file is in there
464 local_file.SetFile(resolved_path, FileSpec::Style::native);
465 FileSystem::Instance().Resolve(local_file);
466 if (FileSystem::Instance().Exists(local_file))
467 return error;
468
469 // Else fall back to the actual path itself
470 local_file.SetFile(platform_file_path, FileSpec::Style::native);
471 FileSystem::Instance().Resolve(local_file);
472 if (FileSystem::Instance().Exists(local_file))
473 return error;
474 }
475 error.SetErrorStringWithFormatv(
476 "unable to locate a platform file for '{0}' in platform '{1}'",
477 platform_file_path, GetPluginName());
478 } else {
479 error.SetErrorString("invalid platform file argument");
480 }
481 return error;
482 }
483
GetSharedModule(const ModuleSpec & module_spec,Process * process,ModuleSP & module_sp,const FileSpecList * module_search_paths_ptr,llvm::SmallVectorImpl<lldb::ModuleSP> * old_modules,bool * did_create_ptr)484 Status PlatformAppleSimulator::GetSharedModule(
485 const ModuleSpec &module_spec, Process *process, ModuleSP &module_sp,
486 const FileSpecList *module_search_paths_ptr,
487 llvm::SmallVectorImpl<lldb::ModuleSP> *old_modules, bool *did_create_ptr) {
488 // For iOS/tvOS/watchOS, the SDK files are all cached locally on the
489 // host system. So first we ask for the file in the cached SDK, then
490 // we attempt to get a shared module for the right architecture with
491 // the right UUID.
492 Status error;
493 ModuleSpec platform_module_spec(module_spec);
494 const FileSpec &platform_file = module_spec.GetFileSpec();
495 error = GetSymbolFile(platform_file, module_spec.GetUUIDPtr(),
496 platform_module_spec.GetFileSpec());
497 if (error.Success()) {
498 error = ResolveExecutable(platform_module_spec, module_sp,
499 module_search_paths_ptr);
500 } else {
501 const bool always_create = false;
502 error = ModuleList::GetSharedModule(module_spec, module_sp,
503 module_search_paths_ptr, old_modules,
504 did_create_ptr, always_create);
505 }
506 if (module_sp)
507 module_sp->SetPlatformFileSpec(platform_file);
508
509 return error;
510 }
511
FindProcesses(const ProcessInstanceInfoMatch & match_info,ProcessInstanceInfoList & process_infos)512 uint32_t PlatformAppleSimulator::FindProcesses(
513 const ProcessInstanceInfoMatch &match_info,
514 ProcessInstanceInfoList &process_infos) {
515 ProcessInstanceInfoList all_osx_process_infos;
516 // First we get all OSX processes
517 const uint32_t n = Host::FindProcesses(match_info, all_osx_process_infos);
518
519 // Now we filter them down to only the matching triples.
520 for (uint32_t i = 0; i < n; ++i) {
521 const ProcessInstanceInfo &proc_info = all_osx_process_infos[i];
522 const llvm::Triple &triple = proc_info.GetArchitecture().GetTriple();
523 if (triple.getOS() == m_os_type &&
524 triple.getEnvironment() == llvm::Triple::Simulator) {
525 process_infos.push_back(proc_info);
526 }
527 }
528 return process_infos.size();
529 }
530
531 /// Whether to skip creating a simulator platform.
shouldSkipSimulatorPlatform(bool force,const ArchSpec * arch)532 static bool shouldSkipSimulatorPlatform(bool force, const ArchSpec *arch) {
533 // If the arch is known not to specify a simulator environment, skip creating
534 // the simulator platform (we can create it later if there's a matching arch).
535 // This avoids very slow xcrun queries for non-simulator archs (the slowness
536 // is due to xcrun not caching negative queries (rdar://74882205)).
537 return !force && arch && arch->IsValid() &&
538 !arch->TripleEnvironmentWasSpecified();
539 }
540
541 static const char *g_ios_plugin_name = "ios-simulator";
542 static const char *g_ios_description = "iPhone simulator platform plug-in.";
543
544 /// IPhone Simulator Plugin.
545 struct PlatformiOSSimulator {
InitializePlatformiOSSimulator546 static void Initialize() {
547 PluginManager::RegisterPlugin(g_ios_plugin_name, g_ios_description,
548 PlatformiOSSimulator::CreateInstance);
549 }
550
TerminatePlatformiOSSimulator551 static void Terminate() {
552 PluginManager::UnregisterPlugin(PlatformiOSSimulator::CreateInstance);
553 }
554
CreateInstancePlatformiOSSimulator555 static PlatformSP CreateInstance(bool force, const ArchSpec *arch) {
556 if (shouldSkipSimulatorPlatform(force, arch))
557 return nullptr;
558
559 return PlatformAppleSimulator::CreateInstance(
560 "PlatformiOSSimulator", g_ios_description,
561 ConstString(g_ios_plugin_name),
562 {llvm::Triple::aarch64, llvm::Triple::x86_64, llvm::Triple::x86},
563 llvm::Triple::IOS,
564 {// Deprecated, but still support Darwin for historical reasons.
565 llvm::Triple::Darwin, llvm::Triple::MacOSX,
566 // IOS is not used for simulator triples, but accept it just in
567 // case.
568 llvm::Triple::IOS},
569 {
570 #ifdef __APPLE__
571 #if __arm64__
572 "arm64e-apple-ios-simulator", "arm64-apple-ios-simulator",
573 "x86_64-apple-ios-simulator", "x86_64h-apple-ios-simulator",
574 #else
575 "x86_64h-apple-ios-simulator", "x86_64-apple-ios-simulator",
576 "i386-apple-ios-simulator",
577 #endif
578 #endif
579 },
580 "iPhoneSimulator.Internal.sdk", "iPhoneSimulator.sdk",
581 XcodeSDK::Type::iPhoneSimulator,
582 CoreSimulatorSupport::DeviceType::ProductFamilyID::iPhone, force, arch);
583 }
584 };
585
586 static const char *g_tvos_plugin_name = "tvos-simulator";
587 static const char *g_tvos_description = "tvOS simulator platform plug-in.";
588
589 /// Apple TV Simulator Plugin.
590 struct PlatformAppleTVSimulator {
InitializePlatformAppleTVSimulator591 static void Initialize() {
592 PluginManager::RegisterPlugin(g_tvos_plugin_name, g_tvos_description,
593 PlatformAppleTVSimulator::CreateInstance);
594 }
595
TerminatePlatformAppleTVSimulator596 static void Terminate() {
597 PluginManager::UnregisterPlugin(PlatformAppleTVSimulator::CreateInstance);
598 }
599
CreateInstancePlatformAppleTVSimulator600 static PlatformSP CreateInstance(bool force, const ArchSpec *arch) {
601 if (shouldSkipSimulatorPlatform(force, arch))
602 return nullptr;
603 return PlatformAppleSimulator::CreateInstance(
604 "PlatformAppleTVSimulator", g_tvos_description,
605 ConstString(g_tvos_plugin_name),
606 {llvm::Triple::aarch64, llvm::Triple::x86_64}, llvm::Triple::TvOS,
607 {llvm::Triple::TvOS},
608 {
609 #ifdef __APPLE__
610 #if __arm64__
611 "arm64e-apple-tvos-simulator", "arm64-apple-tvos-simulator",
612 "x86_64h-apple-tvos-simulator", "x86_64-apple-tvos-simulator",
613 #else
614 "x86_64h-apple-tvos-simulator", "x86_64-apple-tvos-simulator",
615 #endif
616 #endif
617 },
618 "AppleTVSimulator.Internal.sdk", "AppleTVSimulator.sdk",
619 XcodeSDK::Type::AppleTVSimulator,
620 CoreSimulatorSupport::DeviceType::ProductFamilyID::appleTV, force,
621 arch);
622 }
623 };
624
625
626 static const char *g_watchos_plugin_name = "watchos-simulator";
627 static const char *g_watchos_description =
628 "Apple Watch simulator platform plug-in.";
629
630 /// Apple Watch Simulator Plugin.
631 struct PlatformAppleWatchSimulator {
InitializePlatformAppleWatchSimulator632 static void Initialize() {
633 PluginManager::RegisterPlugin(g_watchos_plugin_name, g_watchos_description,
634 PlatformAppleWatchSimulator::CreateInstance);
635 }
636
TerminatePlatformAppleWatchSimulator637 static void Terminate() {
638 PluginManager::UnregisterPlugin(
639 PlatformAppleWatchSimulator::CreateInstance);
640 }
641
CreateInstancePlatformAppleWatchSimulator642 static PlatformSP CreateInstance(bool force, const ArchSpec *arch) {
643 if (shouldSkipSimulatorPlatform(force, arch))
644 return nullptr;
645 return PlatformAppleSimulator::CreateInstance(
646 "PlatformAppleWatchSimulator", g_watchos_description,
647 ConstString(g_watchos_plugin_name),
648 {llvm::Triple::aarch64, llvm::Triple::x86_64, llvm::Triple::x86},
649 llvm::Triple::WatchOS, {llvm::Triple::WatchOS},
650 {
651 #ifdef __APPLE__
652 #if __arm64__
653 "arm64e-apple-watchos-simulator", "arm64-apple-watchos-simulator",
654 #else
655 "x86_64-apple-watchos-simulator", "x86_64h-apple-watchos-simulator",
656 "i386-apple-watchos-simulator",
657 #endif
658 #endif
659 },
660 "WatchSimulator.Internal.sdk", "WatchSimulator.sdk",
661 XcodeSDK::Type::WatchSimulator,
662 CoreSimulatorSupport::DeviceType::ProductFamilyID::appleWatch, force,
663 arch);
664 }
665 };
666
667
668 static unsigned g_initialize_count = 0;
669
670 // Static Functions
Initialize()671 void PlatformAppleSimulator::Initialize() {
672 if (g_initialize_count++ == 0) {
673 PlatformDarwin::Initialize();
674 PlatformiOSSimulator::Initialize();
675 PlatformAppleTVSimulator::Initialize();
676 PlatformAppleWatchSimulator::Initialize();
677 }
678 }
679
Terminate()680 void PlatformAppleSimulator::Terminate() {
681 if (g_initialize_count > 0)
682 if (--g_initialize_count == 0) {
683 PlatformAppleWatchSimulator::Terminate();
684 PlatformAppleTVSimulator::Terminate();
685 PlatformiOSSimulator::Terminate();
686 PlatformDarwin::Terminate();
687 }
688 }
689
690