| ... | ... | @@ -0,0 +1,50 @@ |
| 1 | const testing = @import("std").testing; |
| 2 | const builtin = @import("builtin"); |
| 3 | |
| 4 | // Ported from llvm-project 13.0.0 d7b669b3a30345cfcdb2fde2af6f48aa4b94845d |
| 5 | // |
| 6 | // https://github.com/llvm/llvm-project/blob/llvmorg-13.0.0/compiler-rt/lib/builtins/os_version_check.c |
| 7 | |
| 8 | // The compiler generates calls to __isPlatformVersionAtLeast() when Objective-C's @available |
| 9 | // function is invoked. |
| 10 | // |
| 11 | // Old versions of clang would instead emit calls to __isOSVersionAtLeast(), which is still |
| 12 | // supported in clang's compiler-rt implementation today in case anyone tries to link an object file |
| 13 | // produced with an old clang version. This requires dynamically loading frameworks, parsing a |
| 14 | // system plist file, and generally adds a fair amount of complexity to the implementation and so |
| 15 | // our implementation differs by simply removing that backwards compatability support. We only use |
| 16 | // the newer codepath, which merely calls out to the Darwin _availability_version_check API which is |
| 17 | // available on macOS 10.15+, iOS 13+, tvOS 13+ and watchOS 6+. |
| 18 | |
| 19 | inline fn constructVersion(major: u32, minor: u32, subminor: u32) u32 { |
| 20 | return ((major & 0xffff) << 16) | ((minor & 0xff) << 8) | (subminor & 0xff); |
| 21 | } |
| 22 | |
| 23 | // Darwin-only |
| 24 | pub fn __isPlatformVersionAtLeast(platform: u32, major: u32, minor: u32, subminor: u32) callconv(.C) i32 { |
| 25 | return @boolToInt(_availability_version_check(1, &[_]dyld_build_version_t{ |
| 26 | .{ |
| 27 | .platform = platform, |
| 28 | .version = constructVersion(major, minor, subminor), |
| 29 | }, |
| 30 | })); |
| 31 | } |
| 32 | |
| 33 | // _availability_version_check darwin API support. |
| 34 | const dyld_platform_t = u32; |
| 35 | const dyld_build_version_t = extern struct { |
| 36 | platform: dyld_platform_t, |
| 37 | version: u32, |
| 38 | }; |
| 39 | // Darwin-only |
| 40 | extern "c" fn _availability_version_check(count: u32, versions: [*c]const dyld_build_version_t) bool; |
| 41 | |
| 42 | test "isPlatformVersionAtLeast" { |
| 43 | if (!builtin.os.tag.isDarwin()) return error.SkipZigTest; |
| 44 | |
| 45 | // Note: this test depends on the actual host OS version since it is merely calling into the |
| 46 | // native Darwin API. |
| 47 | const macos_platform_constant = 1; |
| 48 | try testing.expect(__isPlatformVersionAtLeast(macos_platform_constant, 10, 0, 15) == 1); |
| 49 | try testing.expect(__isPlatformVersionAtLeast(macos_platform_constant, 99, 0, 0) == 0); |
| 50 | } |