[llvm] 3974865 - [llvm][TextAPI] Handle implicitly upgraded deployment versions

Cyndy Ishida via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 23 15:01:06 PDT 2023


Author: Cyndy Ishida
Date: 2023-03-23T14:58:41-07:00
New Revision: 397486566e995a019c249784b1d07c53b6ac670d

URL: https://github.com/llvm/llvm-project/commit/397486566e995a019c249784b1d07c53b6ac670d
DIFF: https://github.com/llvm/llvm-project/commit/397486566e995a019c249784b1d07c53b6ac670d.diff

LOG: [llvm][TextAPI] Handle implicitly upgraded deployment versions

Sometimes the clang driver will receive a target triple where the
deployment version is too low to support the platform + arch. In those
cases, the compiler upgrades the final minOS which is what gets recorded
ultimately by the linker in LC_BUILD_VERSION. TextAPI should also reuse
this logic for capturing minOS in recorded TBDv5 files.

Reviewed By: ributzka

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

Added: 
    

Modified: 
    llvm/include/llvm/TextAPI/Platform.h
    llvm/include/llvm/TextAPI/Target.h
    llvm/lib/TextAPI/Platform.cpp
    llvm/lib/TextAPI/TextStubV5.cpp
    llvm/unittests/TextAPI/TextStubV5Tests.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/TextAPI/Platform.h b/llvm/include/llvm/TextAPI/Platform.h
index d4225ca533fc0..834f833306d1b 100644
--- a/llvm/include/llvm/TextAPI/Platform.h
+++ b/llvm/include/llvm/TextAPI/Platform.h
@@ -14,6 +14,7 @@
 
 #include "llvm/ADT/SmallSet.h"
 #include "llvm/BinaryFormat/MachO.h"
+#include "llvm/Support/VersionTuple.h"
 
 namespace llvm {
 namespace MachO {
@@ -27,6 +28,7 @@ StringRef getPlatformName(PlatformType Platform);
 PlatformType getPlatformFromName(StringRef Name);
 std::string getOSAndEnvironmentName(PlatformType Platform,
                                     std::string Version = "");
+VersionTuple mapToSupportedOSVersion(const Triple &Triple);
 
 } // end namespace MachO.
 } // end namespace llvm.

diff  --git a/llvm/include/llvm/TextAPI/Target.h b/llvm/include/llvm/TextAPI/Target.h
index dc0e4f92ae802..0ab2783fc60c5 100644
--- a/llvm/include/llvm/TextAPI/Target.h
+++ b/llvm/include/llvm/TextAPI/Target.h
@@ -33,7 +33,7 @@ class Target {
       : Arch(Arch), Platform(Platform), MinDeployment(MinDeployment) {}
   explicit Target(const llvm::Triple &Triple)
       : Arch(mapToArchitecture(Triple)), Platform(mapToPlatformType(Triple)),
-        MinDeployment(Triple.getOSVersion()) {}
+        MinDeployment(mapToSupportedOSVersion(Triple)) {}
 
   static llvm::Expected<Target> create(StringRef Target);
 

diff  --git a/llvm/lib/TextAPI/Platform.cpp b/llvm/lib/TextAPI/Platform.cpp
index 673fcb764bf86..a432462c82e33 100644
--- a/llvm/lib/TextAPI/Platform.cpp
+++ b/llvm/lib/TextAPI/Platform.cpp
@@ -132,5 +132,12 @@ std::string getOSAndEnvironmentName(PlatformType Platform,
   llvm_unreachable("Unknown llvm::MachO::PlatformType enum");
 }
 
+VersionTuple mapToSupportedOSVersion(const Triple &Triple) {
+  const VersionTuple MinSupportedOS = Triple.getMinimumSupportedOSVersion();
+  if (MinSupportedOS > Triple.getOSVersion())
+    return MinSupportedOS;
+  return Triple.getOSVersion();
+}
+
 } // end namespace MachO.
 } // end namespace llvm.

diff  --git a/llvm/lib/TextAPI/TextStubV5.cpp b/llvm/lib/TextAPI/TextStubV5.cpp
index a9355fabe2202..ade4c867fa49d 100644
--- a/llvm/lib/TextAPI/TextStubV5.cpp
+++ b/llvm/lib/TextAPI/TextStubV5.cpp
@@ -293,8 +293,10 @@ Expected<TargetList> getTargetsSection(const Object *Section) {
     if (!TargetOrErr)
       return make_error<JSONStubError>(getParseErrorMsg(TBDKey::Target));
     TargetOrErr->MinDeployment = Version;
-
-    IFTargets.push_back(*TargetOrErr);
+    // Convert to LLVM::Triple to accurately compute minOS + platform + arch
+    // pairing.
+    IFTargets.push_back(
+        MachO::Target(Triple(getTargetTripleName(*TargetOrErr))));
   }
   return std::move(IFTargets);
 }

diff  --git a/llvm/unittests/TextAPI/TextStubV5Tests.cpp b/llvm/unittests/TextAPI/TextStubV5Tests.cpp
index 3deb38a5a0a3d..b4e8f513daee2 100644
--- a/llvm/unittests/TextAPI/TextStubV5Tests.cpp
+++ b/llvm/unittests/TextAPI/TextStubV5Tests.cpp
@@ -944,6 +944,50 @@ TEST(TBDv5, Target_Simulator) {
   EXPECT_EQ(*File, *WriteResultFile);
 }
 
+TEST(TBDv5, Target_UnsupportedMinOS) {
+  static const char TBDv5File[] = R"({ 
+"tapi_tbd_version": 5,
+"main_library": {
+  "target_info": [
+    {
+      "target": "arm64-macos",
+      "min_deployment": "10.14"
+    },
+    {
+      "target": "x86_64-macos",
+      "min_deployment": "10.14" 
+    }
+  ],
+  "install_names":[
+    { "name":"/S/L/F/Foo.framework/Foo" }
+  ]
+}})";
+
+  Expected<TBDFile> Result =
+      TextAPIReader::get(MemoryBufferRef(TBDv5File, "Test.tbd"));
+  EXPECT_TRUE(!!Result);
+  TBDFile File = std::move(Result.get());
+  EXPECT_EQ(FileType::TBD_V5, File->getFileType());
+  TargetList ExpectedTargets = {
+      Target(AK_x86_64, PLATFORM_MACOS, VersionTuple(10, 14)),
+      Target(AK_arm64, PLATFORM_MACOS, VersionTuple(11, 0)),
+  };
+  TargetList Targets{File->targets().begin(), File->targets().end()};
+  llvm::sort(Targets);
+  EXPECT_EQ(Targets, ExpectedTargets);
+
+  SmallString<4096> Buffer;
+  raw_svector_ostream OS(Buffer);
+  Error WriteResult = TextAPIWriter::writeToStream(OS, *File);
+  EXPECT_TRUE(!WriteResult);
+
+  Expected<TBDFile> Output =
+      TextAPIReader::get(MemoryBufferRef(Buffer, "Output.tbd"));
+  EXPECT_TRUE(!!Output);
+  TBDFile WriteResultFile = std::move(Output.get());
+  EXPECT_EQ(*File, *WriteResultFile);
+}
+
 TEST(TBDv5, MisspelledKey) {
   static const char TBDv5File[] = R"({ 
 "tapi_tbd_version": 5,


        


More information about the llvm-commits mailing list