macosx_libfile.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 builded 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. """
  30. import ctypes
  31. import os
  32. import sys
  33. """here the needed const and struct from mach-o header files"""
  34. FAT_MAGIC = 0xcafebabe
  35. FAT_CIGAM = 0xbebafeca
  36. FAT_MAGIC_64 = 0xcafebabf
  37. FAT_CIGAM_64 = 0xbfbafeca
  38. MH_MAGIC = 0xfeedface
  39. MH_CIGAM = 0xcefaedfe
  40. MH_MAGIC_64 = 0xfeedfacf
  41. MH_CIGAM_64 = 0xcffaedfe
  42. LC_VERSION_MIN_MACOSX = 0x24
  43. LC_BUILD_VERSION = 0x32
  44. mach_header_fields = [
  45. ("magic", ctypes.c_uint32), ("cputype", ctypes.c_int),
  46. ("cpusubtype", ctypes.c_int), ("filetype", ctypes.c_uint32),
  47. ("ncmds", ctypes.c_uint32), ("sizeofcmds", ctypes.c_uint32),
  48. ("flags", ctypes.c_uint32)
  49. ]
  50. """
  51. struct mach_header {
  52. uint32_t magic; /* mach magic number identifier */
  53. cpu_type_t cputype; /* cpu specifier */
  54. cpu_subtype_t cpusubtype; /* machine specifier */
  55. uint32_t filetype; /* type of file */
  56. uint32_t ncmds; /* number of load commands */
  57. uint32_t sizeofcmds; /* the size of all the load commands */
  58. uint32_t flags; /* flags */
  59. };
  60. typedef integer_t cpu_type_t;
  61. typedef integer_t cpu_subtype_t;
  62. """
  63. mach_header_fields_64 = mach_header_fields + [("reserved", ctypes.c_uint32)]
  64. """
  65. struct mach_header_64 {
  66. uint32_t magic; /* mach magic number identifier */
  67. cpu_type_t cputype; /* cpu specifier */
  68. cpu_subtype_t cpusubtype; /* machine specifier */
  69. uint32_t filetype; /* type of file */
  70. uint32_t ncmds; /* number of load commands */
  71. uint32_t sizeofcmds; /* the size of all the load commands */
  72. uint32_t flags; /* flags */
  73. uint32_t reserved; /* reserved */
  74. };
  75. """
  76. fat_header_fields = [("magic", ctypes.c_uint32), ("nfat_arch", ctypes.c_uint32)]
  77. """
  78. struct fat_header {
  79. uint32_t magic; /* FAT_MAGIC or FAT_MAGIC_64 */
  80. uint32_t nfat_arch; /* number of structs that follow */
  81. };
  82. """
  83. fat_arch_fields = [
  84. ("cputype", ctypes.c_int), ("cpusubtype", ctypes.c_int),
  85. ("offset", ctypes.c_uint32), ("size", ctypes.c_uint32),
  86. ("align", ctypes.c_uint32)
  87. ]
  88. """
  89. struct fat_arch {
  90. cpu_type_t cputype; /* cpu specifier (int) */
  91. cpu_subtype_t cpusubtype; /* machine specifier (int) */
  92. uint32_t offset; /* file offset to this object file */
  93. uint32_t size; /* size of this object file */
  94. uint32_t align; /* alignment as a power of 2 */
  95. };
  96. """
  97. fat_arch_64_fields = [
  98. ("cputype", ctypes.c_int), ("cpusubtype", ctypes.c_int),
  99. ("offset", ctypes.c_uint64), ("size", ctypes.c_uint64),
  100. ("align", ctypes.c_uint32), ("reserved", ctypes.c_uint32)
  101. ]
  102. """
  103. struct fat_arch_64 {
  104. cpu_type_t cputype; /* cpu specifier (int) */
  105. cpu_subtype_t cpusubtype; /* machine specifier (int) */
  106. uint64_t offset; /* file offset to this object file */
  107. uint64_t size; /* size of this object file */
  108. uint32_t align; /* alignment as a power of 2 */
  109. uint32_t reserved; /* reserved */
  110. };
  111. """
  112. segment_base_fields = [("cmd", ctypes.c_uint32), ("cmdsize", ctypes.c_uint32)]
  113. """base for reading segment info"""
  114. segment_command_fields = [
  115. ("cmd", ctypes.c_uint32), ("cmdsize", ctypes.c_uint32),
  116. ("segname", ctypes.c_char * 16), ("vmaddr", ctypes.c_uint32),
  117. ("vmsize", ctypes.c_uint32), ("fileoff", ctypes.c_uint32),
  118. ("filesize", ctypes.c_uint32), ("maxprot", ctypes.c_int),
  119. ("initprot", ctypes.c_int), ("nsects", ctypes.c_uint32),
  120. ("flags", ctypes.c_uint32),
  121. ]
  122. """
  123. struct segment_command { /* for 32-bit architectures */
  124. uint32_t cmd; /* LC_SEGMENT */
  125. uint32_t cmdsize; /* includes sizeof section structs */
  126. char segname[16]; /* segment name */
  127. uint32_t vmaddr; /* memory address of this segment */
  128. uint32_t vmsize; /* memory size of this segment */
  129. uint32_t fileoff; /* file offset of this segment */
  130. uint32_t filesize; /* amount to map from the file */
  131. vm_prot_t maxprot; /* maximum VM protection */
  132. vm_prot_t initprot; /* initial VM protection */
  133. uint32_t nsects; /* number of sections in segment */
  134. uint32_t flags; /* flags */
  135. };
  136. typedef int vm_prot_t;
  137. """
  138. segment_command_fields_64 = [
  139. ("cmd", ctypes.c_uint32), ("cmdsize", ctypes.c_uint32),
  140. ("segname", ctypes.c_char * 16), ("vmaddr", ctypes.c_uint64),
  141. ("vmsize", ctypes.c_uint64), ("fileoff", ctypes.c_uint64),
  142. ("filesize", ctypes.c_uint64), ("maxprot", ctypes.c_int),
  143. ("initprot", ctypes.c_int), ("nsects", ctypes.c_uint32),
  144. ("flags", ctypes.c_uint32),
  145. ]
  146. """
  147. struct segment_command_64 { /* for 64-bit architectures */
  148. uint32_t cmd; /* LC_SEGMENT_64 */
  149. uint32_t cmdsize; /* includes sizeof section_64 structs */
  150. char segname[16]; /* segment name */
  151. uint64_t vmaddr; /* memory address of this segment */
  152. uint64_t vmsize; /* memory size of this segment */
  153. uint64_t fileoff; /* file offset of this segment */
  154. uint64_t filesize; /* amount to map from the file */
  155. vm_prot_t maxprot; /* maximum VM protection */
  156. vm_prot_t initprot; /* initial VM protection */
  157. uint32_t nsects; /* number of sections in segment */
  158. uint32_t flags; /* flags */
  159. };
  160. """
  161. version_min_command_fields = segment_base_fields + \
  162. [("version", ctypes.c_uint32), ("sdk", ctypes.c_uint32)]
  163. """
  164. struct version_min_command {
  165. uint32_t cmd; /* LC_VERSION_MIN_MACOSX or
  166. LC_VERSION_MIN_IPHONEOS or
  167. LC_VERSION_MIN_WATCHOS or
  168. LC_VERSION_MIN_TVOS */
  169. uint32_t cmdsize; /* sizeof(struct min_version_command) */
  170. uint32_t version; /* X.Y.Z is encoded in nibbles xxxx.yy.zz */
  171. uint32_t sdk; /* X.Y.Z is encoded in nibbles xxxx.yy.zz */
  172. };
  173. """
  174. build_version_command_fields = segment_base_fields + \
  175. [("platform", ctypes.c_uint32), ("minos", ctypes.c_uint32),
  176. ("sdk", ctypes.c_uint32), ("ntools", ctypes.c_uint32)]
  177. """
  178. struct build_version_command {
  179. uint32_t cmd; /* LC_BUILD_VERSION */
  180. uint32_t cmdsize; /* sizeof(struct build_version_command) plus */
  181. /* ntools * sizeof(struct build_tool_version) */
  182. uint32_t platform; /* platform */
  183. uint32_t minos; /* X.Y.Z is encoded in nibbles xxxx.yy.zz */
  184. uint32_t sdk; /* X.Y.Z is encoded in nibbles xxxx.yy.zz */
  185. uint32_t ntools; /* number of tool entries following this */
  186. };
  187. """
  188. def swap32(x):
  189. return (((x << 24) & 0xFF000000) |
  190. ((x << 8) & 0x00FF0000) |
  191. ((x >> 8) & 0x0000FF00) |
  192. ((x >> 24) & 0x000000FF))
  193. def get_base_class_and_magic_number(lib_file, seek=None):
  194. if seek is None:
  195. seek = lib_file.tell()
  196. else:
  197. lib_file.seek(seek)
  198. magic_number = ctypes.c_uint32.from_buffer_copy(
  199. lib_file.read(ctypes.sizeof(ctypes.c_uint32))).value
  200. # Handle wrong byte order
  201. if magic_number in [FAT_CIGAM, FAT_CIGAM_64, MH_CIGAM, MH_CIGAM_64]:
  202. if sys.byteorder == "little":
  203. BaseClass = ctypes.BigEndianStructure
  204. else:
  205. BaseClass = ctypes.LittleEndianStructure
  206. magic_number = swap32(magic_number)
  207. else:
  208. BaseClass = ctypes.Structure
  209. lib_file.seek(seek)
  210. return BaseClass, magic_number
  211. def read_data(struct_class, lib_file):
  212. return struct_class.from_buffer_copy(lib_file.read(
  213. ctypes.sizeof(struct_class)))
  214. def extract_macosx_min_system_version(path_to_lib):
  215. with open(path_to_lib, "rb") as lib_file:
  216. BaseClass, magic_number = get_base_class_and_magic_number(lib_file, 0)
  217. if magic_number not in [FAT_MAGIC, FAT_MAGIC_64, MH_MAGIC, MH_MAGIC_64]:
  218. return
  219. if magic_number in [FAT_MAGIC, FAT_CIGAM_64]:
  220. class FatHeader(BaseClass):
  221. _fields_ = fat_header_fields
  222. fat_header = read_data(FatHeader, lib_file)
  223. if magic_number == FAT_MAGIC:
  224. class FatArch(BaseClass):
  225. _fields_ = fat_arch_fields
  226. else:
  227. class FatArch(BaseClass):
  228. _fields_ = fat_arch_64_fields
  229. fat_arch_list = [read_data(FatArch, lib_file) for _ in range(fat_header.nfat_arch)]
  230. versions_list = []
  231. for el in fat_arch_list:
  232. try:
  233. version = read_mach_header(lib_file, el.offset)
  234. if version is not None:
  235. versions_list.append(version)
  236. except ValueError:
  237. pass
  238. if len(versions_list) > 0:
  239. return max(versions_list)
  240. else:
  241. return None
  242. else:
  243. try:
  244. return read_mach_header(lib_file, 0)
  245. except ValueError:
  246. """when some error during read library files"""
  247. return None
  248. def read_mach_header(lib_file, seek=None):
  249. """
  250. This funcition parse mach-O header and extract
  251. information about minimal system version
  252. :param lib_file: reference to opened library file with pointer
  253. """
  254. if seek is not None:
  255. lib_file.seek(seek)
  256. base_class, magic_number = get_base_class_and_magic_number(lib_file)
  257. arch = "32" if magic_number == MH_MAGIC else "64"
  258. class SegmentBase(base_class):
  259. _fields_ = segment_base_fields
  260. if arch == "32":
  261. class MachHeader(base_class):
  262. _fields_ = mach_header_fields
  263. else:
  264. class MachHeader(base_class):
  265. _fields_ = mach_header_fields_64
  266. mach_header = read_data(MachHeader, lib_file)
  267. for _i in range(mach_header.ncmds):
  268. pos = lib_file.tell()
  269. segment_base = read_data(SegmentBase, lib_file)
  270. lib_file.seek(pos)
  271. if segment_base.cmd == LC_VERSION_MIN_MACOSX:
  272. class VersionMinCommand(base_class):
  273. _fields_ = version_min_command_fields
  274. version_info = read_data(VersionMinCommand, lib_file)
  275. return parse_version(version_info.version)
  276. elif segment_base.cmd == LC_BUILD_VERSION:
  277. class VersionBuild(base_class):
  278. _fields_ = build_version_command_fields
  279. version_info = read_data(VersionBuild, lib_file)
  280. return parse_version(version_info.minos)
  281. else:
  282. lib_file.seek(pos + segment_base.cmdsize)
  283. continue
  284. def parse_version(version):
  285. x = (version & 0xffff0000) >> 16
  286. y = (version & 0x0000ff00) >> 8
  287. z = (version & 0x000000ff)
  288. return x, y, z
  289. def calculate_macosx_platform_tag(archive_root, platform_tag):
  290. """
  291. Calculate proper macosx platform tag basing on files which are included to wheel
  292. Example platform tag `macosx-10.14-x86_64`
  293. """
  294. prefix, base_version, suffix = platform_tag.split('-')
  295. base_version = tuple([int(x) for x in base_version.split(".")])
  296. if len(base_version) >= 2:
  297. base_version = base_version[0:2]
  298. assert len(base_version) == 2
  299. if "MACOSX_DEPLOYMENT_TARGET" in os.environ:
  300. deploy_target = tuple([int(x) for x in os.environ[
  301. "MACOSX_DEPLOYMENT_TARGET"].split(".")])
  302. if len(deploy_target) >= 2:
  303. deploy_target = deploy_target[0:2]
  304. if deploy_target < base_version:
  305. sys.stderr.write(
  306. "[WARNING] MACOSX_DEPLOYMENT_TARGET is set to a lower value ({}) than the "
  307. "version on which the Python interpreter was compiled ({}), and will be "
  308. "ignored.\n".format('.'.join(str(x) for x in deploy_target),
  309. '.'.join(str(x) for x in base_version))
  310. )
  311. else:
  312. base_version = deploy_target
  313. assert len(base_version) == 2
  314. start_version = base_version
  315. versions_dict = {}
  316. for (dirpath, dirnames, filenames) in os.walk(archive_root):
  317. for filename in filenames:
  318. if filename.endswith('.dylib') or filename.endswith('.so'):
  319. lib_path = os.path.join(dirpath, filename)
  320. min_ver = extract_macosx_min_system_version(lib_path)
  321. if min_ver is not None:
  322. versions_dict[lib_path] = min_ver[0:2]
  323. if len(versions_dict) > 0:
  324. base_version = max(base_version, max(versions_dict.values()))
  325. # macosx platform tag do not support minor bugfix release
  326. fin_base_version = "_".join([str(x) for x in base_version])
  327. if start_version < base_version:
  328. problematic_files = [k for k, v in versions_dict.items() if v > start_version]
  329. problematic_files = "\n".join(problematic_files)
  330. if len(problematic_files) == 1:
  331. files_form = "this file"
  332. else:
  333. files_form = "these files"
  334. error_message = \
  335. "[WARNING] This wheel needs a higher macOS version than {} " \
  336. "To silence this warning, set MACOSX_DEPLOYMENT_TARGET to at least " +\
  337. fin_base_version + " or recreate " + files_form + " with lower " \
  338. "MACOSX_DEPLOYMENT_TARGET: \n" + problematic_files
  339. if "MACOSX_DEPLOYMENT_TARGET" in os.environ:
  340. error_message = error_message.format("is set in MACOSX_DEPLOYMENT_TARGET variable.")
  341. else:
  342. error_message = error_message.format(
  343. "the version your Python interpreter is compiled against.")
  344. sys.stderr.write(error_message)
  345. platform_tag = prefix + "_" + fin_base_version + "_" + suffix
  346. return platform_tag