[Lldb-commits] [lldb] 6e95d51 - Add an XcodeSDK::GetSDKTypeForTriple function

Adrian Prantl via lldb-commits lldb-commits at lists.llvm.org
Thu May 7 11:12:52 PDT 2020


Author: Adrian Prantl
Date: 2020-05-07T11:12:42-07:00
New Revision: 6e95d51ecfab0801b75448b20bd3447645a1142a

URL: https://github.com/llvm/llvm-project/commit/6e95d51ecfab0801b75448b20bd3447645a1142a
DIFF: https://github.com/llvm/llvm-project/commit/6e95d51ecfab0801b75448b20bd3447645a1142a.diff

LOG: Add an XcodeSDK::GetSDKTypeForTriple function

This is something used in swift-lldb, but of general usefulness.

Differential Revision: https://reviews.llvm.org/D79538

<rdar://problem/62684906>

Added: 
    

Modified: 
    lldb/include/lldb/Utility/XcodeSDK.h
    lldb/source/Utility/XcodeSDK.cpp
    lldb/unittests/Utility/XcodeSDKTest.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Utility/XcodeSDK.h b/lldb/include/lldb/Utility/XcodeSDK.h
index 24ab5b1fdf7a..e5a0e0351d83 100644
--- a/lldb/include/lldb/Utility/XcodeSDK.h
+++ b/lldb/include/lldb/Utility/XcodeSDK.h
@@ -14,6 +14,10 @@
 #include "llvm/Support/VersionTuple.h"
 #include <tuple>
 
+namespace llvm {
+class Triple;
+}
+
 namespace lldb_private {
 
 /// An abstraction for Xcode-style SDKs that works like \ref ArchSpec.
@@ -72,6 +76,8 @@ class XcodeSDK {
   static bool SDKSupportsModules(Type desired_type, const FileSpec &sdk_path);
   /// Return the canonical SDK name, such as "macosx" for the macOS SDK.
   static std::string GetCanonicalName(Info info);
+  /// Return the best-matching SDK type for a specific triple.
+  static XcodeSDK::Type GetSDKTypeForTriple(const llvm::Triple &triple);
 };
 
 } // namespace lldb_private

diff  --git a/lldb/source/Utility/XcodeSDK.cpp b/lldb/source/Utility/XcodeSDK.cpp
index fc1fc32b059c..c6f2dda0b2b8 100644
--- a/lldb/source/Utility/XcodeSDK.cpp
+++ b/lldb/source/Utility/XcodeSDK.cpp
@@ -6,10 +6,13 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "lldb/Utility/FileSpec.h"
 #include "lldb/Utility/XcodeSDK.h"
+#include "lldb/Utility/FileSpec.h"
 
 #include "lldb/lldb-types.h"
+
+#include "llvm/ADT/Triple.h"
+
 #include <string>
 
 using namespace lldb;
@@ -190,3 +193,33 @@ bool XcodeSDK::SDKSupportsModules(XcodeSDK::Type desired_type,
     return false;
   return SDKSupportsModules(sdk.GetType(), sdk.GetVersion());
 }
+
+XcodeSDK::Type XcodeSDK::GetSDKTypeForTriple(const llvm::Triple &triple) {
+  using namespace llvm;
+  switch (triple.getOS()) {
+  case Triple::MacOSX:
+  case Triple::Darwin:
+    return XcodeSDK::MacOSX;
+  case Triple::IOS:
+    switch (triple.getEnvironment()) {
+    case Triple::MacABI:
+      return XcodeSDK::MacOSX;
+    case Triple::Simulator:
+      return XcodeSDK::iPhoneSimulator;
+    default:
+      return XcodeSDK::iPhoneOS;
+    }
+  case Triple::TvOS:
+    if (triple.getEnvironment() == Triple::Simulator)
+      return XcodeSDK::AppleTVSimulator;
+    return XcodeSDK::AppleTVOS;
+  case Triple::WatchOS:
+    if (triple.getEnvironment() == Triple::Simulator)
+      return XcodeSDK::WatchSimulator;
+    return XcodeSDK::watchOS;
+  case Triple::Linux:
+    return XcodeSDK::Linux;
+  default:
+    return XcodeSDK::unknown;
+  }
+}

diff  --git a/lldb/unittests/Utility/XcodeSDKTest.cpp b/lldb/unittests/Utility/XcodeSDKTest.cpp
index 0cc353aa1ff7..e89eac2ef2f7 100644
--- a/lldb/unittests/Utility/XcodeSDKTest.cpp
+++ b/lldb/unittests/Utility/XcodeSDKTest.cpp
@@ -12,6 +12,7 @@
 #include "lldb/Utility/XcodeSDK.h"
 
 #include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/Triple.h"
 
 #include <tuple>
 
@@ -147,3 +148,33 @@ TEST(XcodeSDKTest, GetCanonicalName) {
   info.version = llvm::VersionTuple(7, 0);
   EXPECT_EQ("iphoneos7.0.internal", XcodeSDK::GetCanonicalName(info));
 }
+
+TEST(XcodeSDKTest, GetSDKTypeForTriple) {
+  EXPECT_EQ(
+      XcodeSDK::GetSDKTypeForTriple(llvm::Triple("x86_64-apple-macosx10.14")),
+      XcodeSDK::Type::MacOSX);
+  EXPECT_EQ(XcodeSDK::GetSDKTypeForTriple(llvm::Triple("x86_64-apple-darwin")),
+            XcodeSDK::Type::MacOSX);
+  EXPECT_EQ(XcodeSDK::GetSDKTypeForTriple(
+                llvm::Triple("x86_64-apple-ios13.4-simulator")),
+            XcodeSDK::Type::iPhoneSimulator);
+  EXPECT_EQ(XcodeSDK::GetSDKTypeForTriple(llvm::Triple("arm64-apple-ios13.4")),
+            XcodeSDK::Type::iPhoneOS);
+  EXPECT_EQ(XcodeSDK::GetSDKTypeForTriple(
+                llvm::Triple("x86_64-apple-ios13.4-macabi")),
+            XcodeSDK::Type::MacOSX);
+  EXPECT_EQ(XcodeSDK::GetSDKTypeForTriple(
+                llvm::Triple("x86_64-apple-tvos-simulator")),
+            XcodeSDK::Type::AppleTVSimulator);
+  EXPECT_EQ(XcodeSDK::GetSDKTypeForTriple(llvm::Triple("arm64-apple-tvos")),
+            XcodeSDK::Type::AppleTVOS);
+  EXPECT_EQ(XcodeSDK::GetSDKTypeForTriple(
+                llvm::Triple("x86_64-apple-watchos-simulator")),
+            XcodeSDK::Type::WatchSimulator);
+  EXPECT_EQ(XcodeSDK::GetSDKTypeForTriple(llvm::Triple("arm64-apple-watchos")),
+            XcodeSDK::Type::watchOS);
+  EXPECT_EQ(XcodeSDK::GetSDKTypeForTriple(llvm::Triple("x86_64-unknown-linux")),
+            XcodeSDK::Type::Linux);
+  EXPECT_EQ(XcodeSDK::GetSDKTypeForTriple(llvm::Triple("i386-unknown-netbsd")),
+            XcodeSDK::Type::unknown);
+}


        


More information about the lldb-commits mailing list