Skip to content

Commit

Permalink
[Triple] support macOS 11 os version number
Browse files Browse the repository at this point in the history
macOS goes to 11! This commit adds support for the new version number by ensuring
that existing version comparison routines, and the 'darwin' OS identifier
understands the new numbering scheme. It also adds a new utility method
'getCanonicalVersionForOS', which lets users translate some uses of
macOS 10.16 into macOS 11. This utility method will be used in upcoming
clang and swift commits.

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

(cherry picked from commit 1c4a42a)
  • Loading branch information
hyp committed Jun 30, 2020
1 parent d110a84 commit 1206ecd
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 16 deletions.
18 changes: 7 additions & 11 deletions llvm/include/llvm/ADT/Triple.h
Expand Up @@ -19,6 +19,8 @@

namespace llvm {

class VersionTuple;

/// Triple - Helper class for working with autoconf configuration names. For
/// historical reasons, we also call these 'triples' (they used to contain
/// exactly three fields).
Expand Down Expand Up @@ -436,17 +438,7 @@ class Triple {
/// compatibility, which handles supporting skewed version numbering schemes
/// used by the "darwin" triples.
bool isMacOSXVersionLT(unsigned Major, unsigned Minor = 0,
unsigned Micro = 0) const {
assert(isMacOSX() && "Not an OS X triple!");

// If this is OS X, expect a sane version number.
if (getOS() == Triple::MacOSX)
return isOSVersionLT(Major, Minor, Micro);

// Otherwise, compare to the "Darwin" number.
assert(Major == 10 && "Unexpected major version");
return isOSVersionLT(Minor + 4, Micro, 0);
}
unsigned Micro = 0) const;

/// isMacOSX - Is this a Mac OS X triple. For legacy reasons, we support both
/// "darwin" and "osx" as OS X triples.
Expand Down Expand Up @@ -878,6 +870,10 @@ class Triple {
static ArchType getArchTypeForLLVMName(StringRef Str);

/// @}

/// Returns a canonicalized OS version number for the specified OS.
static VersionTuple getCanonicalVersionForOS(OSType OSKind,
const VersionTuple &Version);
};

} // End llvm namespace
Expand Down
47 changes: 42 additions & 5 deletions llvm/lib/Support/Triple.cpp
Expand Up @@ -13,6 +13,7 @@
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/TargetParser.h"
#include "llvm/Support/VersionTuple.h"
#include <cstring>
using namespace llvm;

Expand Down Expand Up @@ -1083,17 +1084,23 @@ bool Triple::getMacOSXVersion(unsigned &Major, unsigned &Minor,
// Darwin version numbers are skewed from OS X versions.
if (Major < 4)
return false;
Micro = 0;
Minor = Major - 4;
Major = 10;
if (Major <= 19) {
Micro = 0;
Minor = Major - 4;
Major = 10;
} else {
Micro = 0;
Minor = 0;
// darwin20+ corresponds to macOS 11+.
Major = 11 + Major - 20;
}
break;
case MacOSX:
// Default to 10.4.
if (Major == 0) {
Major = 10;
Minor = 4;
}
if (Major != 10)
} else if (Major < 10)
return false;
break;
case IOS:
Expand Down Expand Up @@ -1592,6 +1599,23 @@ std::string Triple::merge(const Triple &Other) const {
return Other.str();
}

bool Triple::isMacOSXVersionLT(unsigned Major, unsigned Minor,
unsigned Micro) const {
assert(isMacOSX() && "Not an OS X triple!");

// If this is OS X, expect a sane version number.
if (getOS() == Triple::MacOSX)
return isOSVersionLT(Major, Minor, Micro);

// Otherwise, compare to the "Darwin" number.
if (Major == 10) {
return isOSVersionLT(Minor + 4, Micro, 0);
} else {
assert(Major >= 11 && "Unexpected major version");
return isOSVersionLT(Major - 11 + 20, Minor, Micro);
}
}

StringRef Triple::getARMCPUForArch(StringRef MArch) const {
if (MArch.empty())
MArch = getArchName();
Expand Down Expand Up @@ -1654,3 +1678,16 @@ StringRef Triple::getARMCPUForArch(StringRef MArch) const {

llvm_unreachable("invalid arch name");
}

VersionTuple Triple::getCanonicalVersionForOS(OSType OSKind,
const VersionTuple &Version) {
switch (OSKind) {
case MacOSX:
// macOS 10.16 is canonicalized to macOS 11.
if (Version == VersionTuple(10, 16))
return VersionTuple(11, 0);
LLVM_FALLTHROUGH;
default:
return Version;
}
}
60 changes: 60 additions & 0 deletions llvm/unittests/ADT/TripleTest.cpp
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "llvm/ADT/Triple.h"
#include "llvm/Support/VersionTuple.h"
#include "gtest/gtest.h"

using namespace llvm;
Expand Down Expand Up @@ -1207,6 +1208,44 @@ TEST(TripleTest, getOSVersion) {
EXPECT_EQ((unsigned)0, Minor);
EXPECT_EQ((unsigned)0, Micro);

T = Triple("x86_64-apple-macos11.0");
EXPECT_TRUE(T.isMacOSX());
EXPECT_FALSE(T.isiOS());
EXPECT_FALSE(T.isArch16Bit());
EXPECT_FALSE(T.isArch32Bit());
EXPECT_TRUE(T.isArch64Bit());
T.getMacOSXVersion(Major, Minor, Micro);
EXPECT_EQ((unsigned)11, Major);
EXPECT_EQ((unsigned)0, Minor);
EXPECT_EQ((unsigned)0, Micro);

T = Triple("arm64-apple-macosx11.5.8");
EXPECT_TRUE(T.isMacOSX());
EXPECT_FALSE(T.isiOS());
EXPECT_FALSE(T.isArch16Bit());
EXPECT_FALSE(T.isArch32Bit());
EXPECT_TRUE(T.isArch64Bit());
T.getMacOSXVersion(Major, Minor, Micro);
EXPECT_EQ((unsigned)11, Major);
EXPECT_EQ((unsigned)5, Minor);
EXPECT_EQ((unsigned)8, Micro);

// 10.16 forms a valid triple, even though it's not
// a version of a macOS.
T = Triple("x86_64-apple-macos10.16");
EXPECT_TRUE(T.isMacOSX());
T.getMacOSXVersion(Major, Minor, Micro);
EXPECT_EQ((unsigned)10, Major);
EXPECT_EQ((unsigned)16, Minor);
EXPECT_EQ((unsigned)0, Micro);

T = Triple("x86_64-apple-darwin20");
EXPECT_TRUE(T.isMacOSX());
T.getMacOSXVersion(Major, Minor, Micro);
EXPECT_EQ((unsigned)11, Major);
EXPECT_EQ((unsigned)0, Minor);
EXPECT_EQ((unsigned)0, Micro);

T = Triple("armv7-apple-ios");
EXPECT_FALSE(T.isMacOSX());
EXPECT_TRUE(T.isiOS());
Expand Down Expand Up @@ -1258,6 +1297,27 @@ TEST(TripleTest, getOSVersion) {
EXPECT_FALSE(T.isSimulatorEnvironment());
}

TEST(TripleTest, isMacOSVersionLT) {
Triple T = Triple("x86_64-apple-macos11");
EXPECT_TRUE(T.isMacOSXVersionLT(11, 1, 0));
EXPECT_FALSE(T.isMacOSXVersionLT(10, 15, 0));

T = Triple("x86_64-apple-darwin20");
EXPECT_TRUE(T.isMacOSXVersionLT(11, 1, 0));
EXPECT_FALSE(T.isMacOSXVersionLT(11, 0, 0));
EXPECT_FALSE(T.isMacOSXVersionLT(10, 15, 0));
}

TEST(TripleTest, CanonicalizeOSVersion) {
EXPECT_EQ(VersionTuple(10, 15, 4),
Triple::getCanonicalVersionForOS(Triple::MacOSX,
VersionTuple(10, 15, 4)));
EXPECT_EQ(VersionTuple(11, 0), Triple::getCanonicalVersionForOS(
Triple::MacOSX, VersionTuple(10, 16)));
EXPECT_EQ(VersionTuple(20),
Triple::getCanonicalVersionForOS(Triple::Darwin, VersionTuple(20)));
}

TEST(TripleTest, FileFormat) {
EXPECT_EQ(Triple::ELF, Triple("i686-unknown-linux-gnu").getObjectFormat());
EXPECT_EQ(Triple::ELF, Triple("i686-unknown-freebsd").getObjectFormat());
Expand Down

0 comments on commit 1206ecd

Please sign in to comment.