Skip to content

Commit cd7933d

Browse files
committed
[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 (cherry picked from commit 3974865)
1 parent 0a6fe05 commit cd7933d

File tree

5 files changed

+58
-3
lines changed

5 files changed

+58
-3
lines changed

llvm/include/llvm/TextAPI/Platform.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "llvm/ADT/SmallSet.h"
1616
#include "llvm/BinaryFormat/MachO.h"
17+
#include "llvm/Support/VersionTuple.h"
1718

1819
namespace llvm {
1920
namespace MachO {
@@ -27,6 +28,7 @@ StringRef getPlatformName(PlatformType Platform);
2728
PlatformType getPlatformFromName(StringRef Name);
2829
std::string getOSAndEnvironmentName(PlatformType Platform,
2930
std::string Version = "");
31+
VersionTuple mapToSupportedOSVersion(const Triple &Triple);
3032

3133
} // end namespace MachO.
3234
} // end namespace llvm.

llvm/include/llvm/TextAPI/Target.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Target {
3333
: Arch(Arch), Platform(Platform), MinDeployment(MinDeployment) {}
3434
explicit Target(const llvm::Triple &Triple)
3535
: Arch(mapToArchitecture(Triple)), Platform(mapToPlatformType(Triple)),
36-
MinDeployment(Triple.getOSVersion()) {}
36+
MinDeployment(mapToSupportedOSVersion(Triple)) {}
3737

3838
static llvm::Expected<Target> create(StringRef Target);
3939

llvm/lib/TextAPI/Platform.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,5 +132,12 @@ std::string getOSAndEnvironmentName(PlatformType Platform,
132132
llvm_unreachable("Unknown llvm::MachO::PlatformType enum");
133133
}
134134

135+
VersionTuple mapToSupportedOSVersion(const Triple &Triple) {
136+
const VersionTuple MinSupportedOS = Triple.getMinimumSupportedOSVersion();
137+
if (MinSupportedOS > Triple.getOSVersion())
138+
return MinSupportedOS;
139+
return Triple.getOSVersion();
140+
}
141+
135142
} // end namespace MachO.
136143
} // end namespace llvm.

llvm/lib/TextAPI/TextStubV5.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,10 @@ Expected<TargetList> getTargetsSection(const Object *Section) {
293293
if (!TargetOrErr)
294294
return make_error<JSONStubError>(getParseErrorMsg(TBDKey::Target));
295295
TargetOrErr->MinDeployment = Version;
296-
297-
IFTargets.push_back(*TargetOrErr);
296+
// Convert to LLVM::Triple to accurately compute minOS + platform + arch
297+
// pairing.
298+
IFTargets.push_back(
299+
MachO::Target(Triple(getTargetTripleName(*TargetOrErr))));
298300
}
299301
return std::move(IFTargets);
300302
}

llvm/unittests/TextAPI/TextStubV5Tests.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,50 @@ TEST(TBDv5, Target_Simulator) {
944944
EXPECT_EQ(*File, *WriteResultFile);
945945
}
946946

947+
TEST(TBDv5, Target_UnsupportedMinOS) {
948+
static const char TBDv5File[] = R"({
949+
"tapi_tbd_version": 5,
950+
"main_library": {
951+
"target_info": [
952+
{
953+
"target": "arm64-macos",
954+
"min_deployment": "10.14"
955+
},
956+
{
957+
"target": "x86_64-macos",
958+
"min_deployment": "10.14"
959+
}
960+
],
961+
"install_names":[
962+
{ "name":"/S/L/F/Foo.framework/Foo" }
963+
]
964+
}})";
965+
966+
Expected<TBDFile> Result =
967+
TextAPIReader::get(MemoryBufferRef(TBDv5File, "Test.tbd"));
968+
EXPECT_TRUE(!!Result);
969+
TBDFile File = std::move(Result.get());
970+
EXPECT_EQ(FileType::TBD_V5, File->getFileType());
971+
TargetList ExpectedTargets = {
972+
Target(AK_x86_64, PLATFORM_MACOS, VersionTuple(10, 14)),
973+
Target(AK_arm64, PLATFORM_MACOS, VersionTuple(11, 0)),
974+
};
975+
TargetList Targets{File->targets().begin(), File->targets().end()};
976+
llvm::sort(Targets);
977+
EXPECT_EQ(Targets, ExpectedTargets);
978+
979+
SmallString<4096> Buffer;
980+
raw_svector_ostream OS(Buffer);
981+
Error WriteResult = TextAPIWriter::writeToStream(OS, *File);
982+
EXPECT_TRUE(!WriteResult);
983+
984+
Expected<TBDFile> Output =
985+
TextAPIReader::get(MemoryBufferRef(Buffer, "Output.tbd"));
986+
EXPECT_TRUE(!!Output);
987+
TBDFile WriteResultFile = std::move(Output.get());
988+
EXPECT_EQ(*File, *WriteResultFile);
989+
}
990+
947991
TEST(TBDv5, MisspelledKey) {
948992
static const char TBDv5File[] = R"({
949993
"tapi_tbd_version": 5,

0 commit comments

Comments
 (0)