macosx_libfile.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. """
  2. This module contains function to analyse dynamic library
  3. headers to extract system information
  4. Currently only for MacOSX
  5. Library file on macosx system starts with Mach-O or Fat field.
  6. This can be distinguish by first 32 bites and it is called magic number.
  7. Proper value of magic number is with suffix _MAGIC. Suffix _CIGAM means
  8. reversed bytes order.
  9. Both fields can occur in two types: 32 and 64 bytes.
  10. FAT field inform that this library contains few version of library
  11. (typically for different types version). It contains
  12. information where Mach-O headers starts.
  13. Each section started with Mach-O header contains one library
  14. (So if file starts with this field it contains only one version).
  15. After filed Mach-O there are section fields.
  16. Each of them starts with two fields:
  17. cmd - magic number for this command
  18. cmdsize - total size occupied by this section information.
  19. In this case only sections LC_VERSION_MIN_MACOSX (for macosx 10.13 and earlier)
  20. and LC_BUILD_VERSION (for macosx 10.14 and newer) are interesting,
  21. because them contains information about minimal system version.
  22. Important remarks:
  23. - For fat files this implementation looks for maximum number version.
  24. It not check if it is 32 or 64 and do not compare it with currently built package.
  25. So it is possible to false report higher version that needed.
  26. - All structures signatures are taken form macosx header files.
  27. - I think that binary format will be more stable than `otool` output.
  28. and if apple introduce some changes both implementation will need to be updated.
  29. - The system compile will set the deployment target no lower than
  30. 11.0 for arm64 builds. For "Universal 2" builds use the x86_64 deployment
  31. target when the arm64 target is 11.0.
  32. """
  33. from __future__ import annotations
  34. import ctypes
  35. import os
  36. import sys
  37. """here the needed const and struct from mach-o header files"""
  38. FAT_MAGIC = 0xCAFEBABE
  39. FAT_CIGAM = 0xBEBAFECA
  40. FAT_MAGIC_64 = 0xCAFEBABF
  41. FAT_CIGAM_64 = 0xBFBAFECA
  42. MH_MAGIC = 0xFEEDFACE
  43. MH_CIGAM = 0xCEFAEDFE
  44. MH_MAGIC_64 = 0xFEEDFACF
  45. MH_CIGAM_64 = 0xCFFAEDFE
  46. LC_VERSION_MIN_MACOSX = 0x24
  47. LC_BUILD_VERSION = 0x32
  48. CPU_TYPE_ARM64 = 0x0100000C
  49. mach_header_fields = [
  50. ("magic", ctypes.c_uint32),
  51. ("cputype", ctypes.c_int),
  52. ("cpusubtype", ctypes.c_int),
  53. ("filetype", ctypes.c_uint32),
  54. ("ncmds", ctypes.c_uint32),
  55. ("sizeofcmds", ctypes.c_uint32),
  56. ("flags", ctypes.c_uint32),
  57. ]
  58. """
  59. struct mach_header {
  60. uint32_t magic; /* mach magic number identifier */
  61. cpu_type_t cputype; /* cpu specifier */
  62. cpu_subtype_t cpusubtype; /* machine specifier */
  63. uint32_t filetype; /* type of file */
  64. uint32_t ncmds; /* number of load commands */
  65. uint32_t sizeofcmds; /* the size of all the load commands */
  66. uint32_t flags; /* flags */
  67. };
  68. typedef integer_t cpu_type_t;
  69. typedef integer_t cpu_subtype_t;
  70. """
  71. mach_header_fields_64 = mach_header_fields + [("reserved", ctypes.c_uint32)]
  72. """
  73. struct mach_header_64 {
  74. uint32_t magic; /* mach magic number identifier */
  75. cpu_type_t cputype; /* cpu specifier */
  76. cpu_subtype_t cpusubtype; /* machine specifier */
  77. uint32_t filetype; /* type of file */
  78. uint32_t ncmds; /* number of load commands */
  79. uint32_t sizeofcmds; /* the size of all the load commands */
  80. uint32_t flags; /* flags */
  81. uint32_t reserved; /* reserved */
  82. };
  83. """
  84. fat_header_fields = [("magic", ctypes.c_uint32), ("nfat_arch", ctypes.c_uint32)]
  85. """
  86. struct fat_header {
  87. uint32_t magic; /* FAT_MAGIC or FAT_MAGIC_64 */
  88. uint32_t nfat_arch; /* number of structs that follow */
  89. };
  90. """
  91. fat_arch_fields = [
  92. ("cputype", ctypes.c_int),
  93. ("cpusubtype", ctypes.c_int),
  94. ("offset", ctypes.c_uint32),
  95. ("size", ctypes.c_uint32),
  96. ("align", ctypes.c_uint32),
  97. ]
  98. """
  99. struct fat_arch {
  100. cpu_type_t cputype; /* cpu specifier (int) */
  101. cpu_subtype_t cpusubtype; /* machine specifier (int) */
  102. uint32_t offset; /* file offset to this object file */
  103. uint32_t size; /* size of this object file */
  104. uint32_t align; /* alignment as a power of 2 */
  105. };
  106. """
  107. fat_arch_64_fields = [
  108. ("cputype", ctypes.c_int),
  109. ("cpusubtype", ctypes.c_int),
  110. ("offset", ctypes.c_uint64),
  111. ("size", ctypes.c_uint64),
  112. ("align", ctypes.c_uint32),
  113. ("reserved", ctypes.c_uint32),
  114. ]
  115. """
  116. struct fat_arch_64 {
  117. cpu_type_t cputype; /* cpu specifier (int) */
  118. cpu_subtype_t cpusubtype; /* machine specifier (int) */
  119. uint64_t offset; /* file offset to this object file */
  120. uint64_t size; /* size of this object file */
  121. uint32_t align; /* alignment as a power of 2 */
  122. uint32_t reserved; /* reserved */
  123. };
  124. """
  125. segment_base_fields = [("cmd", ctypes.c_uint32), ("cmdsize", ctypes.c_uint32)]
  126. """base for reading segment info"""
  127. segment_command_fields = [
  128. ("cmd", ctypes.c_uint32),
  129. ("cmdsize", ctypes.c_uint32),
  130. ("segname", ctypes.c_char * 16),
  131. ("vmaddr", ctypes.c_uint32),
  132. ("vmsize", ctypes.c_uint32),
  133. ("fileoff", ctypes.c_uint32),
  134. ("filesize", ctypes.c_uint32),
  135. ("maxprot", ctypes.c_int),
  136. ("initprot", ctypes.c_int),
  137. ("nsects", ctypes.c_uint32),
  138. ("flags", ctypes.c_uint32),
  139. ]
  140. """
  141. struct segment_command { /* for 32-bit architectures */
  142. uint32_t cmd; /* LC_SEGMENT */
  143. uint32_t cmdsize; /* includes sizeof section structs */
  144. char segname[16]; /* segment name */
  145. uint32_t vmaddr; /* memory address of this segment */
  146. uint32_t vmsize; /* memory size of this segment */
  147. uint32_t fileoff; /* file offset of this segment */
  148. uint32_t filesize; /* amount to map from the file */
  149. vm_prot_t maxprot; /* maximum VM protection */
  150. vm_prot_t initprot; /* initial VM protection */
  151. uint32_t nsects; /* number of sections in segment */
  152. uint32_t flags; /* flags */
  153. };
  154. typedef int vm_prot_t;
  155. """
  156. segment_command_fields_64 = [
  157. ("cmd", ctypes.c_uint32),
  158. ("cmdsize", ctypes.c_uint32),
  159. ("segname", ctypes.c_char * 16),
  160. ("vmaddr", ctypes.c_uint64),
  161. ("vmsize", ctypes.c_uint64),
  162. ("fileoff", ctypes.c_uint64),
  163. ("filesize", ctypes.c_uint64),
  164. ("maxprot", ctypes.c_int),
  165. ("initprot", ctypes.c_int),
  166. ("nsects", ctypes.c_uint32),
  167. ("flags", ctypes.c_uint32),
  168. ]
  169. """
  170. struct segment_command_64 { /* for 64-bit architectures */
  171. uint32_t cmd; /* LC_SEGMENT_64 */
  172. uint32_t cmdsize; /* includes sizeof section_64 structs */
  173. char segname[16]; /* segment name */
  174. uint64_t vmaddr; /* memory address of this segment */
  175. uint64_t vmsize; /* memory size of this segment */
  176. uint64_t fileoff; /* file offset of this segment */
  177. uint64_t filesize; /* amount to map from the file */
  178. vm_prot_t maxprot; /* maximum VM protection */
  179. vm_prot_t initprot; /* initial VM protection */
  180. uint32_t nsects; /* number of sections in segment */
  181. uint32_t flags; /* flags */
  182. };
  183. """
  184. version_min_command_fields = segment_base_fields + [
  185. ("version", ctypes.c_uint32),
  186. ("sdk", ctypes.c_uint32),
  187. ]
  188. """
  189. struct version_min_command {
  190. uint32_t cmd; /* LC_VERSION_MIN_MACOSX or
  191. LC_VERSION_MIN_IPHONEOS or
  192. LC_VERSION_MIN_WATCHOS or
  193. LC_VERSION_MIN_TVOS */
  194. uint32_t cmdsize; /* sizeof(struct min_version_command) */
  195. uint32_t version; /* X.Y.Z is encoded in nibbles xxxx.yy.zz */
  196. uint32_t sdk; /* X.Y.Z is encoded in nibbles xxxx.yy.zz */
  197. };
  198. """
  199. build_version_command_fields = segment_base_fields + [
  200. ("platform", ctypes.c_uint32),
  201. ("minos", ctypes.c_uint32),
  202. ("sdk", ctypes.c_uint32),
  203. ("ntools", ctypes.c_uint32),
  204. ]
  205. """
  206. struct build_version_command {
  207. uint32_t cmd; /* LC_BUILD_VERSION */
  208. uint32_t cmdsize; /* sizeof(struct build_version_command) plus */
  209. /* ntools * sizeof(struct build_tool_version) */
  210. uint32_t platform; /* platform */
  211. uint32_t minos; /* X.Y.Z is encoded in nibbles xxxx.yy.zz */
  212. uint32_t sdk; /* X.Y.Z is encoded in nibbles xxxx.yy.zz */
  213. uint32_t ntools; /* number of tool entries following this */
  214. };
  215. """
  216. def swap32(x):
  217. return (
  218. ((x << 24) & 0xFF000000)
  219. | ((x << 8) & 0x00FF0000)
  220. | ((x >> 8) & 0x0000FF00)
  221. | ((x >> 24) & 0x000000FF)
  222. )
  223. def get_base_class_and_magic_number(lib_file, seek=None):
  224. if seek is None:
  225. seek = lib_file.tell()
  226. else:
  227. lib_file.seek(seek)
  228. magic_number = ctypes.c_uint32.from_buffer_copy(
  229. lib_file.read(ctypes.sizeof(ctypes.c_uint32))
  230. ).value
  231. # Handle wrong byte order
  232. if magic_number in [FAT_CIGAM, FAT_CIGAM_64, MH_CIGAM, MH_CIGAM_64]:
  233. if sys.byteorder == "little":
  234. BaseClass = ctypes.BigEndianStructure
  235. else:
  236. BaseClass = ctypes.LittleEndianStructure
  237. magic_number = swap32(magic_number)
  238. else:
  239. BaseClass = ctypes.Structure
  240. lib_file.seek(seek)
  241. return BaseClass, magic_number
  242. def read_data(struct_class, lib_file):
  243. return struct_class.from_buffer_copy(lib_file.read(ctypes.sizeof(struct_class)))
  244. def extract_macosx_min_system_version(path_to_lib):
  245. with open(path_to_lib, "rb") as lib_file:
  246. BaseClass, magic_number = get_base_class_and_magic_number(lib_file, 0)
  247. if magic_number not in [FAT_MAGIC, FAT_MAGIC_64, MH_MAGIC, MH_MAGIC_64]:
  248. return
  249. if magic_number in [FAT_MAGIC, FAT_CIGAM_64]:
  250. class FatHeader(BaseClass):
  251. _fields_ = fat_header_fields
  252. fat_header = read_data(FatHeader, lib_file)
  253. if magic_number == FAT_MAGIC:
  254. class FatArch(BaseClass):
  255. _fields_ = fat_arch_fields
  256. else:
  257. class FatArch(BaseClass):
  258. _fields_ = fat_arch_64_fields
  259. fat_arch_list = [
  260. read_data(FatArch, lib_file) for _ in range(fat_header.nfat_arch)
  261. ]
  262. versions_list = []
  263. for el in fat_arch_list:
  264. try:
  265. version = read_mach_header(lib_file, el.offset)
  266. if version is not None:
  267. if el.cputype == CPU_TYPE_ARM64 and len(fat_arch_list) != 1:
  268. # Xcode will not set the deployment target below 11.0.0
  269. # for the arm64 architecture. Ignore the arm64 deployment
  270. # in fat binaries when the target is 11.0.0, that way
  271. # the other architectures can select a lower deployment
  272. # target.
  273. # This is safe because there is no arm64 variant for
  274. # macOS 10.15 or earlier.
  275. if version == (11, 0, 0):
  276. continue
  277. versions_list.append(version)
  278. except ValueError:
  279. pass
  280. if len(versions_list) > 0:
  281. return max(versions_list)
  282. else:
  283. return None
  284. else:
  285. try:
  286. return read_mach_header(lib_file, 0)
  287. except ValueError:
  288. """when some error during read library files"""
  289. return None
  290. def read_mach_header(lib_file, seek=None):
  291. """
  292. This funcition parse mach-O header and extract
  293. information about minimal system version
  294. :param lib_file: reference to opened library file with pointer
  295. """
  296. if seek is not None:
  297. lib_file.seek(seek)
  298. base_class, magic_number = get_base_class_and_magic_number(lib_file)
  299. arch = "32" if magic_number == MH_MAGIC else "64"
  300. class SegmentBase(base_class):
  301. _fields_ = segment_base_fields
  302. if arch == "32":
  303. class MachHeader(base_class):
  304. _fields_ = mach_header_fields
  305. else:
  306. class MachHeader(base_class):
  307. _fields_ = mach_header_fields_64
  308. mach_header = read_data(MachHeader, lib_file)
  309. for _i in range(mach_header.ncmds):
  310. pos = lib_file.tell()
  311. segment_base = read_data(SegmentBase, lib_file)
  312. lib_file.seek(pos)
  313. if segment_base.cmd == LC_VERSION_MIN_MACOSX:
  314. class VersionMinCommand(base_class):
  315. _fields_ = version_min_command_fields
  316. version_info = read_data(VersionMinCommand, lib_file)
  317. return parse_version(version_info.version)
  318. elif segment_base.cmd == LC_BUILD_VERSION:
  319. class VersionBuild(base_class):
  320. _fields_ = build_version_command_fields
  321. version_info = read_data(VersionBuild, lib_file)
  322. return parse_version(version_info.minos)
  323. else:
  324. lib_file.seek(pos + segment_base.cmdsize)
  325. continue
  326. def parse_version(version):
  327. x = (version & 0xFFFF0000) >> 16
  328. y = (version & 0x0000FF00) >> 8
  329. z = version & 0x000000FF
  330. return x, y, z
  331. def calculate_macosx_platform_tag(archive_root, platform_tag):
  332. """
  333. Calculate proper macosx platform tag basing on files which are included to wheel
  334. Example platform tag `macosx-10.14-x86_64`
  335. """
  336. prefix, base_version, suffix = platform_tag.split("-")
  337. base_version = tuple(int(x) for x in base_version.split("."))
  338. base_version = base_version[:2]
  339. if base_version[0] > 10:
  340. base_version = (base_version[0], 0)
  341. assert len(base_version) == 2
  342. if "MACOSX_DEPLOYMENT_TARGET" in os.environ:
  343. deploy_target = tuple(
  344. int(x) for x in os.environ["MACOSX_DEPLOYMENT_TARGET"].split(".")
  345. )
  346. deploy_target = deploy_target[:2]
  347. if deploy_target[0] > 10:
  348. deploy_target = (deploy_target[0], 0)
  349. if deploy_target < base_version:
  350. sys.stderr.write(
  351. "[WARNING] MACOSX_DEPLOYMENT_TARGET is set to a lower value ({}) than "
  352. "the version on which the Python interpreter was compiled ({}), and "
  353. "will be ignored.\n".format(
  354. ".".join(str(x) for x in deploy_target),
  355. ".".join(str(x) for x in base_version),
  356. )
  357. )
  358. else:
  359. base_version = deploy_target
  360. assert len(base_version) == 2
  361. start_version = base_version
  362. versions_dict = {}
  363. for dirpath, _dirnames, filenames in os.walk(archive_root):
  364. for filename in filenames:
  365. if filename.endswith(".dylib") or filename.endswith(".so"):
  366. lib_path = os.path.join(dirpath, filename)
  367. min_ver = extract_macosx_min_system_version(lib_path)
  368. if min_ver is not None:
  369. min_ver = min_ver[0:2]
  370. if min_ver[0] > 10:
  371. min_ver = (min_ver[0], 0)
  372. versions_dict[lib_path] = min_ver
  373. if len(versions_dict) > 0:
  374. base_version = max(base_version, max(versions_dict.values()))
  375. # macosx platform tag do not support minor bugfix release
  376. fin_base_version = "_".join([str(x) for x in base_version])
  377. if start_version < base_version:
  378. problematic_files = [k for k, v in versions_dict.items() if v > start_version]
  379. problematic_files = "\n".join(problematic_files)
  380. if len(problematic_files) == 1:
  381. files_form = "this file"
  382. else:
  383. files_form = "these files"
  384. error_message = (
  385. "[WARNING] This wheel needs a higher macOS version than {} "
  386. "To silence this warning, set MACOSX_DEPLOYMENT_TARGET to at least "
  387. + fin_base_version
  388. + " or recreate "
  389. + files_form
  390. + " with lower "
  391. "MACOSX_DEPLOYMENT_TARGET: \n" + problematic_files
  392. )
  393. if "MACOSX_DEPLOYMENT_TARGET" in os.environ:
  394. error_message = error_message.format(
  395. "is set in MACOSX_DEPLOYMENT_TARGET variable."
  396. )
  397. else:
  398. error_message = error_message.format(
  399. "the version your Python interpreter is compiled against."
  400. )
  401. sys.stderr.write(error_message)
  402. platform_tag = prefix + "_" + fin_base_version + "_" + suffix
  403. return platform_tag