xref: /aosp_15_r20/external/aws-crt-java/codebuild/macos_compatibility_check.py (revision 3c7ae9de214676c52d19f01067dc1a404272dc11)
1import sys
2import subprocess
3import os
4import re
5
6
7def main():
8    if sys.platform != 'darwin':
9        print("WARNING: Not running on macos. Skip the compatibility validation.")
10        # Exit quietly if run on a non-darwin machine.
11        sys.exit(0)
12
13    # Default target macos version setup in pom.xml > ${cmake.min_osx_version}
14    supported_version = "10.9"
15    arch = "x86_64"
16
17
18    # otool result has a different format between arm and x64
19    # for arm: we check for "minos"
20    # for x64: The format will be:
21    #
22    # Load command 8
23    # cmd LC_VERSION_MIN_MACOSX
24    #   cmdsize 16
25    #   version 10.9
26    #       sdk 12.1
27    # Load command 9
28    otool_cmd = "otool -l target/cmake-build/lib/osx/{}/cruntime/libaws-crt-jni.dylib | grep -A3 \'LC_VERSION_MIN_MACOSX\' | grep -E version | tr -s ' ' | cut -f3 -d' ' | tr -d '[:space:]'".format(arch)
29
30    if len(sys.argv) > 1:
31        # Parsing the macos archtecture
32        arch = sys.argv[1]
33    else:
34        # If the archtecture is not set, set from system call
35        arch = os.uname().machine
36        print("uname result {}".format(arch))
37
38    if re.match(r'^(aarch64|armv[6-8]|arm64)', arch):
39        arch = "armv8"
40        # The oldest version we can target on arm64 is 11.0
41        supported_version = "11.0"
42        otool_cmd = "otool -l target/cmake-build/lib/osx/{}/cruntime/libaws-crt-jni.dylib | grep -E minos | tr -s ' ' | cut -f3 -d' ' | tr -d ' ' | tr -d '[:space:]'".format(arch)
43
44    print("Start to validate the build binary for MacOS with architecture {}, expected min os version: {}".format(arch,supported_version))
45    result = subprocess.check_output(otool_cmd, shell=True).decode("utf-8")
46
47    if result != supported_version:
48        # Failed
49        print("Failed the compatibility validation on MacOS architecture {}, expected '{}' and built '{}'".format(arch, supported_version, result))
50        sys.exit(1)
51
52    print("Pass the compatibility validation on MacOS architecture {} with min supported os version '{}'".format(arch,result))
53    sys.exit(0)
54
55if __name__ == "__main__":
56    main()
57