zig/lib/std/special/compiler_rt/os_version_check.zig
John Schmidt 9ee67b967b stage2: avoid inferred struct in os_version_check.zig
Before this commit, compiling an empty main with Stage 2 on macOS x86_64 results in

```
../stage2/bin/zig build-exe -ODebug -fLLVM empty_main.zig
error: sub-compilation of compiler_rt failed
    [...]/zig/stage2/lib/zig/std/special/compiler_rt/os_version_check.zig:26:10: error: TODO: Sema.zirStructInit for runtime-known struct values
```

By assigning the value to a variable we can sidestep the issue for now.
2022-01-26 00:48:05 -05:00

50 lines
2.2 KiB
Zig

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