authorgravatar for stephen@hexops.comStephen Gutekanst <stephen@hexops.com> 2021-11-29 12:54:23-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-11-29 14:54:23-05:00
logb613210140f649b11232fae8bad16867d122d0cb
treeb775c1bea3af00d772429b6b421b3c382d2b4ad7
parentda0ea909bcce59e265bf5651e5281b6af2216eae
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

compiler_rt: implement __isPlatformVersionAtLeast (Objective-C @available expressions) for Darwin (#10232)


3 files changed, 57 insertions(+), 0 deletions(-)

CMakeLists.txt+1
...@@ -497,6 +497,7 @@ set(ZIG_STAGE2_SOURCES...@@ -497,6 +497,7 @@ set(ZIG_STAGE2_SOURCES
497 "${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/muloti4.zig"497 "${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/muloti4.zig"
498 "${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/multi3.zig"498 "${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/multi3.zig"
499 "${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/negXf2.zig"499 "${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/negXf2.zig"
500 "${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/os_version_check.zig"
500 "${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/popcountdi2.zig"501 "${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/popcountdi2.zig"
501 "${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/shift.zig"502 "${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/shift.zig"
502 "${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/stack_probe.zig"503 "${CMAKE_SOURCE_DIR}/lib/std/special/compiler_rt/stack_probe.zig"
lib/std/special/compiler_rt.zig+6
...@@ -7,6 +7,7 @@ const abi = builtin.abi;...@@ -7,6 +7,7 @@ const abi = builtin.abi;
77
8const is_gnu = abi.isGnu();8const is_gnu = abi.isGnu();
9const is_mingw = os_tag == .windows and is_gnu;9const is_mingw = os_tag == .windows and is_gnu;
10const is_darwin = std.Target.Os.Tag.isDarwin(os_tag);
1011
11const linkage = if (is_test)12const linkage = if (is_test)
12 std.builtin.GlobalLinkage.Internal13 std.builtin.GlobalLinkage.Internal
...@@ -336,6 +337,11 @@ comptime {...@@ -336,6 +337,11 @@ comptime {
336 const __popcountdi2 = @import("compiler_rt/popcountdi2.zig").__popcountdi2;337 const __popcountdi2 = @import("compiler_rt/popcountdi2.zig").__popcountdi2;
337 @export(__popcountdi2, .{ .name = "__popcountdi2", .linkage = linkage });338 @export(__popcountdi2, .{ .name = "__popcountdi2", .linkage = linkage });
338339
340 if (is_darwin) {
341 const __isPlatformVersionAtLeast = @import("compiler_rt/os_version_check.zig").__isPlatformVersionAtLeast;
342 @export(__isPlatformVersionAtLeast, .{ .name = "__isPlatformVersionAtLeast", .linkage = linkage });
343 }
344
339 const __mulsi3 = @import("compiler_rt/int.zig").__mulsi3;345 const __mulsi3 = @import("compiler_rt/int.zig").__mulsi3;
340 @export(__mulsi3, .{ .name = "__mulsi3", .linkage = linkage });346 @export(__mulsi3, .{ .name = "__mulsi3", .linkage = linkage });
341 const __muldi3 = @import("compiler_rt/muldi3.zig").__muldi3;347 const __muldi3 = @import("compiler_rt/muldi3.zig").__muldi3;
lib/std/special/compiler_rt/os_version_check.zig created+50
...@@ -0,0 +1,50 @@
1const testing = @import("std").testing;
2const 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
19inline fn constructVersion(major: u32, minor: u32, subminor: u32) u32 {
20 return ((major & 0xffff) << 16) | ((minor & 0xff) << 8) | (subminor & 0xff);
21}
22
23// Darwin-only
24pub 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.
34const dyld_platform_t = u32;
35const dyld_build_version_t = extern struct {
36 platform: dyld_platform_t,
37 version: u32,
38};
39// Darwin-only
40extern "c" fn _availability_version_check(count: u32, versions: [*c]const dyld_build_version_t) bool;
41
42test "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}