31 lines
1.2 KiB
C
31 lines
1.2 KiB
C
// Runtime helpers that Xcode's compiler-rt normally provides (absent from the Linux toolchain).
|
|
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
#include <sys/sysctl.h>
|
|
|
|
static int32_t os_major = -1, os_minor, os_patch;
|
|
|
|
static void load_version(void) {
|
|
char buf[32] = {0};
|
|
size_t n = sizeof buf - 1;
|
|
os_major = 0;
|
|
if (sysctlbyname("kern.osproductversion", buf, &n, NULL, 0) != 0) return;
|
|
char *p = buf;
|
|
os_major = (int32_t)strtol(p, &p, 10);
|
|
if (*p == '.') os_minor = (int32_t)strtol(p + 1, &p, 10);
|
|
if (*p == '.') os_patch = (int32_t)strtol(p + 1, &p, 10);
|
|
}
|
|
|
|
/// Backs `if #available(...)` / `@available` checks. Platform is ignored: this binary only runs on iOS.
|
|
int32_t __isPlatformVersionAtLeast(uint32_t platform, uint32_t major, uint32_t minor, uint32_t subminor) {
|
|
(void)platform;
|
|
if (os_major < 0) load_version();
|
|
if (os_major != (int32_t)major) return os_major > (int32_t)major;
|
|
if (os_minor != (int32_t)minor) return os_minor > (int32_t)minor;
|
|
return os_patch >= (int32_t)subminor;
|
|
}
|
|
|
|
/// Older form used by `__builtin_available` in C.
|
|
int32_t __isOSVersionAtLeast(int32_t major, int32_t minor, int32_t subminor) {
|
|
return __isPlatformVersionAtLeast(2, (uint32_t)major, (uint32_t)minor, (uint32_t)subminor);
|
|
}
|