"""
This module contains function to analyse dynamic library
headers to extract system information
Currently only for MacOSX
Library file on macosx system starts with Mach-O or Fat field.
This can be distinguish by first 32 bites and it is called magic number.
Proper value of magic number iswith suffix _MAGIC. Suffix _CIGAM means
reversed bytes order.
Both fields can occur in two types: 32 and 64 bytes.
FAT field inform that this library contains few version of library
(typically for different types version). It contains
information where Mach-O headers starts.
Each section started with Mach-O header contains one library
(So if file starts with this field it contains only one version).
After filed Mach-O there are section fields.
Each of them starts with two fields:
cmd - magic number for this command
cmdsize - total size occupied by this section information.
In this case only sections LC_VERSION_MIN_MACOSX (for macosx 10.13 and earlier) and LC_BUILD_VERSION (for macosx 10.14 and newer) are interesting,
because them contains information about minimal system version.
Important remarks:
- For fat files this implementation looks for maximum number version.
It not check if it is 32 or 64 and do not compare it with currently built package.
So it is possible to false report higher version that needed.
- All structures signatures are taken form macosx header files.
- I think that binary format will be more stable than `otool` output. andif apple introduce some changes both implementation will need to be updated.
- The system compile will set the deployment target no lower than
11.0 for arm64 builds. For"Universal 2" builds use the x86_64 deployment
target when the arm64 target is 11.0. """
from __future__ import annotations
import ctypes import os import sys
"""here the needed const and struct from mach-o header files"""
def extract_macosx_min_system_version(path_to_lib): with open(path_to_lib, "rb") as lib_file:
BaseClass, magic_number = get_base_class_and_magic_number(lib_file, 0) if magic_number notin [FAT_MAGIC, FAT_MAGIC_64, MH_MAGIC, MH_MAGIC_64]: return
if magic_number in [FAT_MAGIC, FAT_CIGAM_64]:
class FatHeader(BaseClass):
_fields_ = fat_header_fields
fat_header = read_data(FatHeader, lib_file) if magic_number == FAT_MAGIC:
class FatArch(BaseClass):
_fields_ = fat_arch_fields
else:
class FatArch(BaseClass):
_fields_ = fat_arch_64_fields
fat_arch_list = [
read_data(FatArch, lib_file) for _ in range(fat_header.nfat_arch)
]
versions_list = [] for el in fat_arch_list: try:
version = read_mach_header(lib_file, el.offset) if version isnotNone: if el.cputype == CPU_TYPE_ARM64 and len(fat_arch_list) != 1: # Xcode will not set the deployment target below 11.0.0 # for the arm64 architecture. Ignore the arm64 deployment # in fat binaries when the target is 11.0.0, that way # the other architectures can select a lower deployment # target. # This is safe because there is no arm64 variant for # macOS 10.15 or earlier. if version == (11, 0, 0): continue
versions_list.append(version) except ValueError: pass
if len(versions_list) > 0: return max(versions_list) else: returnNone
else: try: return read_mach_header(lib_file, 0) except ValueError: """when some error during read library files""" returnNone
def read_mach_header(lib_file, seek=None): """
This function parses a Mach-O header and extracts
information about the minimal macOS version.
:param lib_file: reference to opened library file with pointer """
base_class, magic_number = get_base_class_and_magic_number(lib_file, seek)
arch = "32"if magic_number == MH_MAGIC else"64"
class SegmentBase(base_class):
_fields_ = segment_base_fields
if arch == "32":
class MachHeader(base_class):
_fields_ = mach_header_fields
else:
class MachHeader(base_class):
_fields_ = mach_header_fields_64
mach_header = read_data(MachHeader, lib_file) for _i in range(mach_header.ncmds):
pos = lib_file.tell()
segment_base = read_data(SegmentBase, lib_file)
lib_file.seek(pos) if segment_base.cmd == LC_VERSION_MIN_MACOSX:
class VersionMinCommand(base_class):
_fields_ = version_min_command_fields
def parse_version(version):
x = (version & 0xFFFF0000) >> 16
y = (version & 0x0000FF00) >> 8
z = version & 0x000000FF return x, y, z
def calculate_macosx_platform_tag(archive_root, platform_tag): """
Calculate proper macosx platform tag basing on files which are included to wheel
Example platform tag `macosx-10.14-x86_64` """
prefix, base_version, suffix = platform_tag.split("-")
base_version = tuple(int(x) for x in base_version.split("."))
base_version = base_version[:2] if base_version[0] > 10:
base_version = (base_version[0], 0) assert len(base_version) == 2 if"MACOSX_DEPLOYMENT_TARGET"in os.environ:
deploy_target = tuple(
int(x) for x in os.environ["MACOSX_DEPLOYMENT_TARGET"].split(".")
)
deploy_target = deploy_target[:2] if deploy_target[0] > 10:
deploy_target = (deploy_target[0], 0) if deploy_target < base_version:
sys.stderr.write( "[WARNING] MACOSX_DEPLOYMENT_TARGET is set to a lower value ({}) than " "the version on which the Python interpreter was compiled ({}), and " "will be ignored.\n".format( ".".join(str(x) for x in deploy_target), ".".join(str(x) for x in base_version),
)
) else:
base_version = deploy_target
assert len(base_version) == 2
start_version = base_version
versions_dict = {} for dirpath, _dirnames, filenames in os.walk(archive_root): for filename in filenames: if filename.endswith(".dylib") or filename.endswith(".so"):
lib_path = os.path.join(dirpath, filename)
min_ver = extract_macosx_min_system_version(lib_path) if min_ver isnotNone:
min_ver = min_ver[0:2] if min_ver[0] > 10:
min_ver = (min_ver[0], 0)
versions_dict[lib_path] = min_ver
if len(versions_dict) > 0:
base_version = max(base_version, max(versions_dict.values()))
# macosx platform tag do not support minor bugfix release
fin_base_version = "_".join([str(x) for x in base_version]) if start_version < base_version:
problematic_files = [k for k, v in versions_dict.items() if v > start_version]
problematic_files = "\n".join(problematic_files) if len(problematic_files) == 1:
files_form = "this file" else:
files_form = "these files"
error_message = ( "[WARNING] This wheel needs a higher macOS version than {} " "To silence this warning, set MACOSX_DEPLOYMENT_TARGET to at least "
+ fin_base_version
+ " or recreate "
+ files_form
+ " with lower " "MACOSX_DEPLOYMENT_TARGET: \n" + problematic_files
)
if"MACOSX_DEPLOYMENT_TARGET"in os.environ:
error_message = error_message.format( "is set in MACOSX_DEPLOYMENT_TARGET variable."
) else:
error_message = error_message.format( "the version your Python interpreter is compiled against."
)
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.