-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
osversion.go
116 lines (102 loc) · 2.79 KB
/
osversion.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package vz
/*
#cgo darwin CFLAGS: -mmacosx-version-min=11 -x objective-c -fno-objc-arc
#cgo darwin LDFLAGS: -lobjc -framework Foundation
# include "virtualization_helper.h"
*/
import "C"
import (
"errors"
"fmt"
"strconv"
"strings"
"sync"
"syscall"
"golang.org/x/mod/semver"
)
var (
// ErrUnsupportedOSVersion is returned when calling a method which is only
// available in newer macOS versions.
ErrUnsupportedOSVersion = errors.New("unsupported macOS version")
// ErrBuildTargetOSVersion indicates that the API is available but the
// running program has disabled it.
ErrBuildTargetOSVersion = errors.New("unsupported build target macOS version")
)
func macOSAvailable(version float64) error {
if macOSMajorMinorVersion() < version {
return ErrUnsupportedOSVersion
}
return macOSBuildTargetAvailable(version)
}
var (
majorMinorVersion float64
majorMinorVersionOnce interface{ Do(func()) } = &sync.Once{}
// This can be replaced in the test code to enable mock.
// It will not be changed in production.
sysctl = syscall.Sysctl
)
func fetchMajorMinorVersion() (float64, error) {
osver, err := sysctl("kern.osproductversion")
if err != nil {
return 0, err
}
prefix := "v"
majorMinor := strings.TrimPrefix(semver.MajorMinor(prefix+osver), prefix)
version, err := strconv.ParseFloat(majorMinor, 64)
if err != nil {
return 0, err
}
return version, nil
}
func macOSMajorMinorVersion() float64 {
majorMinorVersionOnce.Do(func() {
version, err := fetchMajorMinorVersion()
if err != nil {
panic(err)
}
majorMinorVersion = version
})
return majorMinorVersion
}
var (
maxAllowedVersion int
maxAllowedVersionOnce interface{ Do(func()) } = &sync.Once{}
getMaxAllowedVersion = func() int {
return int(C.mac_os_x_version_max_allowed())
}
)
func fetchMaxAllowedVersion() int {
maxAllowedVersionOnce.Do(func() {
maxAllowedVersion = getMaxAllowedVersion()
})
return maxAllowedVersion
}
// macOSBuildTargetAvailable checks whether the API available in a given version has been compiled.
func macOSBuildTargetAvailable(version float64) error {
allowedVersion := fetchMaxAllowedVersion()
if allowedVersion == 0 {
return fmt.Errorf("undefined __MAC_OS_X_VERSION_MAX_ALLOWED: %w", ErrBuildTargetOSVersion)
}
// FIXME(codehex): smart way
// This list from AvailabilityVersions.h
var target int
switch version {
case 11:
target = 110000 // __MAC_11_0
case 12:
target = 120000 // __MAC_12_0
case 12.3:
target = 120300 // __MAC_12_3
case 13:
target = 130000 // __MAC_13_0
case 14:
target = 140000 // __MAC_14_0
case 15:
target = 150000 // __MAC_15_0
}
if allowedVersion < target {
return fmt.Errorf("%w for %.1f (the binary was built with __MAC_OS_X_VERSION_MAX_ALLOWED=%d; needs recompilation)",
ErrBuildTargetOSVersion, version, allowedVersion)
}
return nil
}