|
| 1 | +from pathlib import Path |
| 2 | +import sys |
| 3 | + |
| 4 | +from huggingface_hub import snapshot_download |
| 5 | +from kernels.utils import CACHE_DIR |
| 6 | +from kernel_abi_check import ( |
| 7 | + BinaryFormat, |
| 8 | + IncompatibleMacOSVersion, |
| 9 | + ObjectFile, |
| 10 | + IncompatibleAbi3Symbol, |
| 11 | + NonAbi3Symbol, |
| 12 | + IncompatibleManylinuxSymbol, |
| 13 | + MissingMacOSVersion, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +def check_kernel( |
| 18 | + *, macos: str, manylinux: str, python_abi: str, repo_id: str, revision: str |
| 19 | +): |
| 20 | + variants_path = ( |
| 21 | + Path( |
| 22 | + snapshot_download( |
| 23 | + repo_id, |
| 24 | + allow_patterns=["build/*"], |
| 25 | + cache_dir=CACHE_DIR, |
| 26 | + revision=revision, |
| 27 | + ) |
| 28 | + ) |
| 29 | + / "build" |
| 30 | + ) |
| 31 | + |
| 32 | + has_issues = False |
| 33 | + for variant_path in variants_path.iterdir(): |
| 34 | + if not variant_path.is_dir(): |
| 35 | + print( |
| 36 | + f"⛔ `build/` must only contain directories, found: {variant_path.name}", |
| 37 | + file=sys.stderr, |
| 38 | + ) |
| 39 | + has_issues = True |
| 40 | + continue |
| 41 | + |
| 42 | + print(f"Checking variant: {variant_path.name}", file=sys.stderr) |
| 43 | + |
| 44 | + indent = 2 |
| 45 | + |
| 46 | + for dylib_path in variant_path.rglob("*.so"): |
| 47 | + print_with_indent( |
| 48 | + indent, |
| 49 | + f"Dynamic library {dylib_path.relative_to(variant_path)}:", |
| 50 | + ) |
| 51 | + |
| 52 | + o = ObjectFile(dylib_path) |
| 53 | + has_issues |= check_abi3(o, python_abi, indent + 2) |
| 54 | + |
| 55 | + # TODO: also check operating system |
| 56 | + if o.format() == BinaryFormat.ELF: |
| 57 | + has_issues |= check_manylinux(o, manylinux, indent + 2) |
| 58 | + elif o.format() == BinaryFormat.MACH_O: |
| 59 | + has_issues |= check_macos(o, macos, indent + 2) |
| 60 | + |
| 61 | + if has_issues: |
| 62 | + sys.exit(1) |
| 63 | + |
| 64 | + |
| 65 | +def check_abi3(object_file: ObjectFile, python_abi: str, indent: int) -> bool: |
| 66 | + has_issues = False |
| 67 | + violations = object_file.check_python_abi(python_abi) |
| 68 | + if violations != []: |
| 69 | + has_issues = True |
| 70 | + print_with_indent( |
| 71 | + indent, |
| 72 | + f"⛔ Found symbols that are incompatible with Python ABI {python_abi}:", |
| 73 | + ) |
| 74 | + for violation in violations: |
| 75 | + if isinstance(violation, IncompatibleAbi3Symbol): |
| 76 | + print_with_indent( |
| 77 | + indent + 3, |
| 78 | + f"{violation.name}: {violation.version_added}", |
| 79 | + ) |
| 80 | + elif isinstance(violation, NonAbi3Symbol): |
| 81 | + print_with_indent( |
| 82 | + indent + 3, |
| 83 | + f"{violation.name}", |
| 84 | + ) |
| 85 | + else: |
| 86 | + print_with_indent(indent, f"🐍 Python ABI {python_abi} compatible") |
| 87 | + |
| 88 | + return has_issues |
| 89 | + |
| 90 | + |
| 91 | +def check_macos(object_file: ObjectFile, macos: str, indent: int) -> bool: |
| 92 | + has_issues = False |
| 93 | + violations = object_file.check_macos(macos) |
| 94 | + if violations != []: |
| 95 | + has_issues = True |
| 96 | + print_with_indent( |
| 97 | + indent, |
| 98 | + f"⛔ Found incompatibility with macOS {macos}:", |
| 99 | + ) |
| 100 | + |
| 101 | + for violation in violations: |
| 102 | + if isinstance(violation, MissingMacOSVersion): |
| 103 | + print_with_indent( |
| 104 | + indent + 3, |
| 105 | + "shared library does not contain macOS version", |
| 106 | + ) |
| 107 | + elif isinstance(violation, IncompatibleMacOSVersion): |
| 108 | + print_with_indent( |
| 109 | + indent + 3, |
| 110 | + f"shared library requires macOS {violation.version}", |
| 111 | + ) |
| 112 | + else: |
| 113 | + print_with_indent(indent, f"🍏 compatible with macOS {macos}") |
| 114 | + |
| 115 | + return has_issues |
| 116 | + |
| 117 | + |
| 118 | +def check_manylinux(object_file: ObjectFile, manylinux: str, indent: int) -> bool: |
| 119 | + has_issues = False |
| 120 | + violations = object_file.check_manylinux(manylinux) |
| 121 | + if violations != []: |
| 122 | + has_issues = True |
| 123 | + print_with_indent( |
| 124 | + indent, |
| 125 | + f"⛔ Found symbols that are incompatible with {manylinux}:", |
| 126 | + ) |
| 127 | + |
| 128 | + for violation in violations: |
| 129 | + if isinstance(violation, IncompatibleManylinuxSymbol): |
| 130 | + print_with_indent( |
| 131 | + indent + 3, |
| 132 | + f"{violation.name}_{violation.dep}: {violation.version}", |
| 133 | + ) |
| 134 | + else: |
| 135 | + print_with_indent(indent, f"🐧 {manylinux} compatible") |
| 136 | + |
| 137 | + return has_issues |
| 138 | + |
| 139 | + |
| 140 | +def print_with_indent(indent: int, message: str): |
| 141 | + print(f"{' ' * indent}{message}", file=sys.stderr) |
0 commit comments