[Lldb-commits] [lldb] 4c6eebf - [lldb/AppleSimulator] Always provide a -simulator environment

Fred Riss via lldb-commits lldb-commits at lists.llvm.org
Mon Jul 27 12:51:22 PDT 2020


Author: Fred Riss
Date: 2020-07-27T12:50:50-07:00
New Revision: 4c6eebf86a0734779cd20473cfcaa9d7c8899298

URL: https://github.com/llvm/llvm-project/commit/4c6eebf86a0734779cd20473cfcaa9d7c8899298
DIFF: https://github.com/llvm/llvm-project/commit/4c6eebf86a0734779cd20473cfcaa9d7c8899298.diff

LOG: [lldb/AppleSimulator] Always provide a -simulator environment

Summary:
This commit is somewhat NFC-ish today as the environment of triples
is not considered when comparing s if one of them is
not set (I plan to change that).

We have made simulator triples unambiguous these days, but the
simulator platforms still advertise triples without the
environment. This wasn't an issue when the sims ran only on
a very different architecure than the real device, but this
has changed with Apple Silicon.

This patch simplifies the way GetSupportedArchitectureAtIndex
is implemented for the sim platforms and adds the environment.
It also trivially adds support for Apple Silicon to those
platforms.

Reviewers: aprantl

Subscribers: lldb-commits

Added: 
    lldb/unittests/Platform/PlatformAppleSimulatorTest.cpp

Modified: 
    lldb/source/Plugins/Platform/MacOSX/PlatformAppleSimulator.cpp
    lldb/source/Plugins/Platform/MacOSX/PlatformAppleSimulator.h
    lldb/source/Plugins/Platform/MacOSX/PlatformAppleTVSimulator.cpp
    lldb/source/Plugins/Platform/MacOSX/PlatformAppleTVSimulator.h
    lldb/source/Plugins/Platform/MacOSX/PlatformAppleWatchSimulator.cpp
    lldb/source/Plugins/Platform/MacOSX/PlatformAppleWatchSimulator.h
    lldb/source/Plugins/Platform/MacOSX/PlatformiOSSimulator.cpp
    lldb/source/Plugins/Platform/MacOSX/PlatformiOSSimulator.h
    lldb/unittests/Platform/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/lldb/source/Plugins/Platform/MacOSX/PlatformAppleSimulator.cpp b/lldb/source/Plugins/Platform/MacOSX/PlatformAppleSimulator.cpp
index bd0a231303bd..0160fb95c58a 100644
--- a/lldb/source/Plugins/Platform/MacOSX/PlatformAppleSimulator.cpp
+++ b/lldb/source/Plugins/Platform/MacOSX/PlatformAppleSimulator.cpp
@@ -253,3 +253,11 @@ CoreSimulatorSupport::Device PlatformAppleSimulator::GetSimulatorDevice() {
     return CoreSimulatorSupport::Device();
 }
 #endif
+
+bool PlatformAppleSimulator::GetSupportedArchitectureAtIndex(uint32_t idx,
+                                                             ArchSpec &arch) {
+  if (idx >= m_supported_triples.size())
+    return false;
+  arch = ArchSpec(m_supported_triples[idx]);
+  return true;
+}

diff  --git a/lldb/source/Plugins/Platform/MacOSX/PlatformAppleSimulator.h b/lldb/source/Plugins/Platform/MacOSX/PlatformAppleSimulator.h
index 8c0174f2946e..6182acaf229a 100644
--- a/lldb/source/Plugins/Platform/MacOSX/PlatformAppleSimulator.h
+++ b/lldb/source/Plugins/Platform/MacOSX/PlatformAppleSimulator.h
@@ -44,6 +44,9 @@ class PlatformAppleSimulator : public PlatformDarwin {
                                lldb_private::Target *target,
                                lldb_private::Status &error) override;
 
+  bool GetSupportedArchitectureAtIndex(uint32_t idx,
+                                       lldb_private::ArchSpec &arch) override;
+
 protected:
   std::mutex m_core_sim_path_mutex;
   llvm::Optional<lldb_private::FileSpec> m_core_simulator_framework_path;
@@ -52,6 +55,9 @@ class PlatformAppleSimulator : public PlatformDarwin {
 
   lldb_private::FileSpec GetCoreSimulatorPath();
 
+  llvm::Triple::OSType m_os_type = llvm::Triple::UnknownOS;
+  llvm::ArrayRef<llvm::StringRef> m_supported_triples = {};
+
   void LoadCoreSimulator();
 
 #if defined(__APPLE__)

diff  --git a/lldb/source/Plugins/Platform/MacOSX/PlatformAppleTVSimulator.cpp b/lldb/source/Plugins/Platform/MacOSX/PlatformAppleTVSimulator.cpp
index 461624a2adaa..27f798b00ebf 100644
--- a/lldb/source/Plugins/Platform/MacOSX/PlatformAppleTVSimulator.cpp
+++ b/lldb/source/Plugins/Platform/MacOSX/PlatformAppleTVSimulator.cpp
@@ -77,6 +77,7 @@ PlatformSP PlatformAppleTVSimulator::CreateInstance(bool force,
   bool create = force;
   if (!create && arch && arch->IsValid()) {
     switch (arch->GetMachine()) {
+    case llvm::Triple::aarch64:
     case llvm::Triple::x86_64: {
       const llvm::Triple &triple = arch->GetTriple();
       switch (triple.getVendor()) {
@@ -144,7 +145,24 @@ const char *PlatformAppleTVSimulator::GetDescriptionStatic() {
 /// Default Constructor
 PlatformAppleTVSimulator::PlatformAppleTVSimulator()
     : PlatformAppleSimulator(
-          CoreSimulatorSupport::DeviceType::ProductFamilyID::appleTV) {}
+          CoreSimulatorSupport::DeviceType::ProductFamilyID::appleTV) {
+#ifdef __APPLE__
+#if __arm64__
+  static const llvm::StringRef supported_triples[] = {
+      "arm64e-apple-tvos-simulator",
+      "arm64-apple-tvos-simulator",
+      "x86_64h-apple-tvos-simulator",
+      "x86_64-apple-tvos-simulator",
+  };
+#else
+  static const llvm::StringRef supported_triples[] = {
+      "x86_64h-apple-tvos-simulator",
+      "x86_64-apple-tvos-simulator",
+  };
+#endif
+  m_supported_triples = supported_triples;
+#endif
+}
 
 /// Destructor.
 ///
@@ -322,19 +340,3 @@ uint32_t PlatformAppleTVSimulator::FindProcesses(
   }
   return process_infos.size();
 }
-
-bool PlatformAppleTVSimulator::GetSupportedArchitectureAtIndex(uint32_t idx,
-                                                               ArchSpec &arch) {
-  static const ArchSpec platform_arch(
-      HostInfo::GetArchitecture(HostInfo::eArchKind64));
-
-  if (idx == 0) {
-    arch = platform_arch;
-    if (arch.IsValid()) {
-      arch.GetTriple().setOS(llvm::Triple::TvOS);
-      arch.GetTriple().setEnvironment(llvm::Triple::Simulator);
-      return true;
-    }
-  }
-  return false;
-}

diff  --git a/lldb/source/Plugins/Platform/MacOSX/PlatformAppleTVSimulator.h b/lldb/source/Plugins/Platform/MacOSX/PlatformAppleTVSimulator.h
index 5a7b0ee0d7dc..a94f94f9f57f 100644
--- a/lldb/source/Plugins/Platform/MacOSX/PlatformAppleTVSimulator.h
+++ b/lldb/source/Plugins/Platform/MacOSX/PlatformAppleTVSimulator.h
@@ -62,9 +62,6 @@ class PlatformAppleTVSimulator : public PlatformAppleSimulator {
   FindProcesses(const lldb_private::ProcessInstanceInfoMatch &match_info,
                 lldb_private::ProcessInstanceInfoList &process_infos) override;
 
-  bool GetSupportedArchitectureAtIndex(uint32_t idx,
-                                       lldb_private::ArchSpec &arch) override;
-
   void
   AddClangModuleCompilationOptions(lldb_private::Target *target,
                                    std::vector<std::string> &options) override {

diff  --git a/lldb/source/Plugins/Platform/MacOSX/PlatformAppleWatchSimulator.cpp b/lldb/source/Plugins/Platform/MacOSX/PlatformAppleWatchSimulator.cpp
index 03a8fcd31360..79f254c43a6a 100644
--- a/lldb/source/Plugins/Platform/MacOSX/PlatformAppleWatchSimulator.cpp
+++ b/lldb/source/Plugins/Platform/MacOSX/PlatformAppleWatchSimulator.cpp
@@ -76,6 +76,7 @@ PlatformSP PlatformAppleWatchSimulator::CreateInstance(bool force,
   bool create = force;
   if (!create && arch && arch->IsValid()) {
     switch (arch->GetMachine()) {
+    case llvm::Triple::aarch64:
     case llvm::Triple::x86_64:
     case llvm::Triple::x86: {
       const llvm::Triple &triple = arch->GetTriple();
@@ -145,7 +146,23 @@ const char *PlatformAppleWatchSimulator::GetDescriptionStatic() {
 /// Default Constructor
 PlatformAppleWatchSimulator::PlatformAppleWatchSimulator()
     : PlatformAppleSimulator(
-          CoreSimulatorSupport::DeviceType::ProductFamilyID::appleWatch) {}
+          CoreSimulatorSupport::DeviceType::ProductFamilyID::appleWatch) {
+#ifdef __APPLE__
+#if __arm64__
+  static const llvm::StringRef supported_triples[] = {
+      "arm64e-apple-watchos-simulator",
+      "arm64-apple-watchos-simulator",
+  };
+#else
+  static const llvm::StringRef supported_triples[] = {
+      "x86_64-apple-watchos-simulator",
+      "x86_64h-apple-watchos-simulator",
+      "i386-apple-watchos-simulator",
+  };
+#endif
+  m_supported_triples = supported_triples;
+#endif
+}
 
 /// Destructor.
 ///
@@ -325,24 +342,3 @@ uint32_t PlatformAppleWatchSimulator::FindProcesses(
   return process_infos.size();
 }
 
-bool PlatformAppleWatchSimulator::GetSupportedArchitectureAtIndex(
-    uint32_t idx, ArchSpec &arch) {
-  if (idx == 0) {
-    arch = HostInfo::GetArchitecture(HostInfo::eArchKind32);
-    if (arch.IsValid()) {
-      arch.GetTriple().setOS(llvm::Triple::WatchOS);
-      arch.GetTriple().setEnvironment(llvm::Triple::Simulator);
-      return true;
-    }
-  }
-
-  if (idx == 1) {
-    arch = HostInfo::GetArchitecture(HostInfo::eArchKind64);
-    if (arch.IsValid()) {
-      arch.GetTriple().setOS(llvm::Triple::WatchOS);
-      arch.GetTriple().setEnvironment(llvm::Triple::Simulator);
-      return true;
-    }
-  }
-  return false;
-}

diff  --git a/lldb/source/Plugins/Platform/MacOSX/PlatformAppleWatchSimulator.h b/lldb/source/Plugins/Platform/MacOSX/PlatformAppleWatchSimulator.h
index 96dcd16ffa99..78b936691b0c 100644
--- a/lldb/source/Plugins/Platform/MacOSX/PlatformAppleWatchSimulator.h
+++ b/lldb/source/Plugins/Platform/MacOSX/PlatformAppleWatchSimulator.h
@@ -62,9 +62,6 @@ class PlatformAppleWatchSimulator : public PlatformAppleSimulator {
   FindProcesses(const lldb_private::ProcessInstanceInfoMatch &match_info,
                 lldb_private::ProcessInstanceInfoList &process_infos) override;
 
-  bool GetSupportedArchitectureAtIndex(uint32_t idx,
-                                       lldb_private::ArchSpec &arch) override;
-
   void
   AddClangModuleCompilationOptions(lldb_private::Target *target,
                                    std::vector<std::string> &options) override {

diff  --git a/lldb/source/Plugins/Platform/MacOSX/PlatformiOSSimulator.cpp b/lldb/source/Plugins/Platform/MacOSX/PlatformiOSSimulator.cpp
index a890d0afdf1e..b73c06fcdc8b 100644
--- a/lldb/source/Plugins/Platform/MacOSX/PlatformiOSSimulator.cpp
+++ b/lldb/source/Plugins/Platform/MacOSX/PlatformiOSSimulator.cpp
@@ -76,6 +76,7 @@ PlatformSP PlatformiOSSimulator::CreateInstance(bool force,
   bool create = force;
   if (!create && arch && arch->IsValid()) {
     switch (arch->GetMachine()) {
+    case llvm::Triple::aarch64:
     case llvm::Triple::x86_64:
     case llvm::Triple::x86: {
       const llvm::Triple &triple = arch->GetTriple();
@@ -148,7 +149,25 @@ const char *PlatformiOSSimulator::GetDescriptionStatic() {
 /// Default Constructor
 PlatformiOSSimulator::PlatformiOSSimulator()
     : PlatformAppleSimulator(
-          CoreSimulatorSupport::DeviceType::ProductFamilyID::iPhone) {}
+          CoreSimulatorSupport::DeviceType::ProductFamilyID::iPhone) {
+#ifdef __APPLE__
+#if __arm64__
+  static const llvm::StringRef supported_triples[] = {
+      "arm64e-apple-ios-simulator",
+      "arm64-apple-ios-simulator",
+      "x86_64-apple-ios-simulator",
+      "x86_64h-apple-ios-simulator",
+  };
+#else
+  static const llvm::StringRef supported_triples[] = {
+      "x86_64h-apple-ios-simulator",
+      "x86_64-apple-ios-simulator",
+      "i386-apple-ios-simulator",
+  };
+#endif
+  m_supported_triples = supported_triples;
+#endif
+}
 
 /// Destructor.
 ///
@@ -328,43 +347,3 @@ PlatformiOSSimulator::FindProcesses(const ProcessInstanceInfoMatch &match_info,
   return process_infos.size();
 }
 
-bool PlatformiOSSimulator::GetSupportedArchitectureAtIndex(uint32_t idx,
-                                                           ArchSpec &arch) {
-  static const ArchSpec platform_arch(
-      HostInfo::GetArchitecture(HostInfo::eArchKindDefault));
-  static const ArchSpec platform_arch64(
-      HostInfo::GetArchitecture(HostInfo::eArchKind64));
-
-  if (idx == 0) {
-    arch = platform_arch;
-    if (arch.IsValid()) {
-      arch.GetTriple().setOS(llvm::Triple::IOS);
-      arch.GetTriple().setEnvironment(llvm::Triple::Simulator);
-      return true;
-    }
-  } else {
-    if (platform_arch.IsExactMatch(platform_arch64)) {
-      // This macosx platform supports both 32 and 64 bit.
-      if (idx == 1) {
-        // 32/64: return "x86_64-apple-macosx" for architecture 1
-        arch = platform_arch64;
-        return true;
-      } else if (idx == 2 || idx == 3) {
-        arch = HostInfo::GetArchitecture(HostInfo::eArchKind32);
-        if (arch.IsValid()) {
-          if (idx == 2)
-            arch.GetTriple().setOS(llvm::Triple::IOS);
-          // 32/64: return "i386-apple-ios" for architecture 2 32/64: return
-          // "i386-apple-macosx" for architecture 3
-          return true;
-        }
-      }
-    } else if (idx == 1) {
-      // This macosx platform supports only 32 bit, so return the *-apple-
-      // macosx version
-      arch = platform_arch;
-      return true;
-    }
-  }
-  return false;
-}

diff  --git a/lldb/source/Plugins/Platform/MacOSX/PlatformiOSSimulator.h b/lldb/source/Plugins/Platform/MacOSX/PlatformiOSSimulator.h
index 4d416d759bd2..982f8e2de5e7 100644
--- a/lldb/source/Plugins/Platform/MacOSX/PlatformiOSSimulator.h
+++ b/lldb/source/Plugins/Platform/MacOSX/PlatformiOSSimulator.h
@@ -64,9 +64,6 @@ class PlatformiOSSimulator : public PlatformAppleSimulator {
   FindProcesses(const lldb_private::ProcessInstanceInfoMatch &match_info,
                 lldb_private::ProcessInstanceInfoList &process_infos) override;
 
-  bool GetSupportedArchitectureAtIndex(uint32_t idx,
-                                       lldb_private::ArchSpec &arch) override;
-
   void
   AddClangModuleCompilationOptions(lldb_private::Target *target,
                                    std::vector<std::string> &options) override {

diff  --git a/lldb/unittests/Platform/CMakeLists.txt b/lldb/unittests/Platform/CMakeLists.txt
index eb7f0a6ca3c4..ca5031b9b43e 100644
--- a/lldb/unittests/Platform/CMakeLists.txt
+++ b/lldb/unittests/Platform/CMakeLists.txt
@@ -1,4 +1,5 @@
 add_lldb_unittest(LLDBPlatformTests
+  PlatformAppleSimulatorTest.cpp
   PlatformDarwinTest.cpp
 
   LINK_LIBS

diff  --git a/lldb/unittests/Platform/PlatformAppleSimulatorTest.cpp b/lldb/unittests/Platform/PlatformAppleSimulatorTest.cpp
new file mode 100644
index 000000000000..0b90380b797c
--- /dev/null
+++ b/lldb/unittests/Platform/PlatformAppleSimulatorTest.cpp
@@ -0,0 +1,74 @@
+//===-- PlatformAppleSimulatorTest.cpp ------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "gtest/gtest.h"
+
+#include "Plugins/Platform/MacOSX/PlatformAppleTVSimulator.h"
+#include "Plugins/Platform/MacOSX/PlatformAppleWatchSimulator.h"
+#include "Plugins/Platform/MacOSX/PlatformiOSSimulator.h"
+#include "TestingSupport/SubsystemRAII.h"
+#include "lldb/Host/FileSystem.h"
+#include "lldb/Host/HostInfo.h"
+#include "lldb/Target/Platform.h"
+
+using namespace lldb;
+using namespace lldb_private;
+
+class PlatformAppleSimulatorTest : public ::testing::Test {
+  SubsystemRAII<FileSystem, HostInfo, PlatformAppleTVSimulator,
+                PlatformiOSSimulator, PlatformAppleWatchSimulator>
+      subsystems;
+};
+
+#ifdef __APPLE__
+
+static void testSimPlatformArchHasSimEnvironment(llvm::StringRef name) {
+  Status error;
+  auto platform_sp = Platform::Create(ConstString(name), error);
+  ASSERT_TRUE(platform_sp);
+  int num_arches = 0;
+
+  while (true) {
+    ArchSpec arch;
+    if (!platform_sp->GetSupportedArchitectureAtIndex(num_arches, arch))
+      break;
+    EXPECT_EQ(arch.GetTriple().getEnvironment(), llvm::Triple::Simulator);
+    num_arches++;
+  }
+
+  EXPECT_GT(num_arches, 0);
+}
+
+TEST_F(PlatformAppleSimulatorTest, TestSimHasSimEnvionament) {
+  testSimPlatformArchHasSimEnvironment("ios-simulator");
+  testSimPlatformArchHasSimEnvironment("tvos-simulator");
+  testSimPlatformArchHasSimEnvironment("watchos-simulator");
+}
+
+TEST_F(PlatformAppleSimulatorTest, TestHostPlatformToSim) {
+  static const ArchSpec platform_arch(
+      HostInfo::GetArchitecture(HostInfo::eArchKindDefault));
+
+  const llvm::Triple::OSType sim_platforms[] = {
+      llvm::Triple::IOS,
+      llvm::Triple::TvOS,
+      llvm::Triple::WatchOS,
+  };
+
+  for (auto sim : sim_platforms) {
+    ArchSpec arch = platform_arch;
+    arch.GetTriple().setOS(sim);
+    arch.GetTriple().setEnvironment(llvm::Triple::Simulator);
+
+    Status error;
+    auto platform_sp = Platform::Create(arch, nullptr, error);
+    EXPECT_TRUE(platform_sp);
+  }
+}
+
+#endif


        


More information about the lldb-commits mailing list