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