Debian Codegraph @

pyqtbuild/bundle/qt_wheel.py

raw
1 # SPDX-License-Identifier: BSD-2-Clause
2
3 # Copyright (c) 2025 Phil Thompson <[email protected]>
4
5
6 import os
7 import shutil
8
9 from sipbuild import UserException
10
11 from . import packages
12 from .verbose import verbose
13 from .wheel import create_wheel, write_record_file
14
15
16 def qt_wheel(package, qt_dir, build_tag, suffix, msvc_runtime, openssl,
17 openssl_dir, exclude, arch, subwheel):
18 """ Create a wheel containing the subset of a Qt installation required for
19 a particular PyQt package.
20 """
21
22 if openssl_dir:
23 openssl_dir = os.path.abspath(openssl_dir)
24
25 # Normalise the name of the package.
26 package_name = package.replace('-', '_')
27
28 # Get the package object.
29 package_factory = packages.__dict__.get(package_name)
30
31 if package_factory is None:
32 raise UserException(f"'{package}' is not a supported package")
33
34 package = package_factory(qt_dir)
35
36 version_str = package.qt_version_str
37 if suffix:
38 version_str += suffix
39
40 # Construct the tag.
41 qt_arch = os.path.basename(qt_dir)
42
43 if qt_arch.startswith('gcc_'):
44 if qt_arch == 'gcc_arm64':
45 wheel_arch = 'aarch64'
46 manylinux = '_2_39'
47 else:
48 wheel_arch = 'x86_64'
49
50 if package.qt_version >= (6, 10, 0):
51 manylinux = '_2_34'
52 elif package.qt_version >= (6, 0, 0):
53 manylinux = '_2_28'
54 else:
55 manylinux = '2014'
56
57 platform_tag = f'manylinux{manylinux}_{wheel_arch}'
58
59 elif qt_arch in ('macos', 'clang_64', 'x86_64', 'arm64'):
60 if package.qt_version < (5, 15, 10) or (6, 0, 0) <= package.qt_version < (6, 2, 0):
61 if arch is not None:
62 raise UserException(
63 "'--arch' may only be specified for Qt v5.15.10 and later or Qt v6.2 and later")
64
65 subarch = 'x86_64'
66 elif arch is None:
67 # Assume it is universal unless the installed Qt architecture is
68 # specific.
69 subarch = qt_arch if qt_arch in ('x86_64', 'arm64') else 'universal2'
70 else:
71 subarch = arch
72
73 if subarch == 'arm64':
74 sdk_version = '11_0'
75 elif package.qt_version[0] == 5:
76 sdk_version = '10_13'
77 else:
78 sdk_version = '10_14'
79
80 platform_tag = 'macosx_{}_{}'.format(sdk_version, subarch)
81
82 elif qt_arch.startswith('msvc'):
83 if qt_arch.endswith('_64'):
84 platform_tag = 'win_amd64'
85 elif qt_arch.endswith('_arm64'):
86 platform_tag = 'win_arm64'
87 else:
88 platform_tag = 'win32'
89
90 else:
91 raise UserException(
92 "Qt architecture '{0}' is unsupported".format(qt_arch))
93
94 tag_parts = ['py3', 'none', platform_tag]
95 tag = '-'.join(tag_parts)
96
97 package_title = package_name.replace('_', '-')
98 qt_version_suffix = '-Qt' + version_str[0]
99
100 # Determine sub-wheel dependent values'
101 subwheel_full_name = f'{package_title}Subwheel{qt_version_suffix}'
102
103 if subwheel is True:
104 package_full_name = subwheel_full_name
105 package_requires = ''
106 elif subwheel is False:
107 package_full_name = package_title + qt_version_suffix
108 package_requires = f'Requires-Dist: {subwheel_full_name} (=={version_str})\n'
109 else:
110 package_full_name = package_title + qt_version_suffix
111 package_requires = ''
112
113 # Construct the name of the wheel.
114 name_parts = [package_full_name.replace('-', '_').lower()]
115 name_parts.append(version_str)
116
117 distinfo_dir = '-'.join(name_parts) + '.dist-info'
118
119 if build_tag:
120 name_parts.append(build_tag)
121
122 name_parts += tag_parts
123
124 wheel_name = '-'.join(name_parts)
125 wheel_path = os.path.abspath(wheel_name + '.whl')
126
127 # Create the directory to contain the wheel contents.
128 shutil.rmtree(wheel_name, ignore_errors=True)
129 os.mkdir(wheel_name)
130
131 saved_cwd = os.getcwd()
132 os.chdir(wheel_name)
133
134 # Bundle the relevant parts of the Qt installation.
135 target_qt_dir = package.get_target_qt_dir()
136 lgpl = package.bundle_qt(target_qt_dir, platform_tag, exclude,
137 ignore_missing=True, bindings=False, subwheel=subwheel)
138
139 if platform_tag in ('win32', 'win_amd64'):
140 # Bundle the MSVC runtime if required.
141 if msvc_runtime:
142 package.bundle_msvc_runtime(target_qt_dir, platform_tag)
143
144 # Bundle OpenSSL if required.
145 if openssl:
146 package.bundle_openssl(target_qt_dir, openssl_dir, platform_tag)
147
148 # Create the .dist-info directory and populate it from the prototypes.
149 os.mkdir(distinfo_dir)
150
151 proto_dir = os.path.join(os.path.dirname(__file__), 'qt_wheel_distinfo')
152 for proto in os.listdir(proto_dir):
153 src = os.path.join(proto_dir, proto)
154 dst = os.path.join(distinfo_dir, proto)
155
156 if proto == 'METADATA':
157 with open(src) as s:
158 metadata = s.read()
159
160 metadata = metadata.replace('@RB_PACKAGE@', package_title)
161 metadata = metadata.replace('@RB_PACKAGE_NAME@', package_full_name)
162 metadata = metadata.replace('@RB_PACKAGE_REQUIRES@',
163 package_requires)
164 metadata = metadata.replace('@RB_VERSION@', version_str)
165 metadata = metadata.replace('@RB_LICENSE@',
166 "LGPL v3" if lgpl else "GPL v3")
167
168 with open(dst, 'w') as d:
169 d.write(metadata)
170 elif proto == 'WHEEL':
171 with open(src) as s:
172 wheel_data = s.read()
173
174 with open(dst, 'w') as d:
175 d.write(wheel_data)
176 d.write('Tag: {}\n'.format(tag))
177
178 if build_tag:
179 d.write('Build: {}\n'.format(build_tag))
180 elif proto.startswith('LICENSE.'):
181 if proto.endswith('.lgpl3' if lgpl else '.gpl3'):
182 shutil.copy(src, os.path.join(distinfo_dir, 'LICENSE'))
183 else:
184 shutil.copy(src, dst)
185
186 # Write the wheel's RECORD file.
187 verbose("Writing the RECORD file")
188 names = write_record_file(distinfo_dir)
189
190 # Create the wheel.
191 verbose("Writing {0}".format(wheel_name))
192 create_wheel(wheel_path, names)
193
194 # Tidy up.
195 os.chdir(saved_cwd)
196 shutil.rmtree(wheel_name)
197
198 verbose("Wheel build complete.")