| 1 |
# SPDX-License-Identifier: BSD-2-Clause |
| 2 |
|
| 3 |
# Copyright (c) 2024 Phil Thompson <[email protected]> |
| 4 |
|
| 5 |
|
| 6 |
import fnmatch |
| 7 |
import os |
| 8 |
import shutil |
| 9 |
import subprocess |
| 10 |
|
| 11 |
from sipbuild import UserException |
| 12 |
|
| 13 |
from .verbose import is_verbose, verbose |
| 14 |
|
| 15 |
|
| 16 |
class VersionedMetadata: |
| 17 |
""" Encapsulate the meta-data for a set of bindings for a particular |
| 18 |
version of Qt. |
| 19 |
""" |
| 20 |
|
| 21 |
def __init__(self, *, version=None, name=None, lib_deps=None, |
| 22 |
other_lib_deps=None, exes=None, files=None, others=None, dll=True, |
| 23 |
qml_names=None, translations=None, excluded_plugins=None, |
| 24 |
lgpl=True, legacy=False, subwheel_files=None): |
| 25 |
""" Initialise the versioned bindings. """ |
| 26 |
|
| 27 |
self._version = version |
| 28 |
self._name = name |
| 29 |
self._lib_deps = {} if lib_deps is None else lib_deps |
| 30 |
self._other_lib_deps = {} if other_lib_deps is None else other_lib_deps |
| 31 |
self._exes = {} if exes is None else exes |
| 32 |
self._files = {} if files is None else files |
| 33 |
self._others = {} if others is None else others |
| 34 |
self._dll = dll |
| 35 |
self._qml_names = qml_names |
| 36 |
self._translations = () if translations is None else translations |
| 37 |
self._excluded_plugins = excluded_plugins |
| 38 |
self._subwheel_files = {} if subwheel_files is None else subwheel_files |
| 39 |
|
| 40 |
self.lgpl = lgpl |
| 41 |
self.legacy = legacy |
| 42 |
|
| 43 |
def bundle(self, name, target_qt_dir, qt_dir, platform_tag, qt_version, |
| 44 |
ignore_missing, subwheel): |
| 45 |
""" Bundle part of Qt as defined by the meta-data. """ |
| 46 |
|
| 47 |
verbose(f"Bundling {name}") |
| 48 |
|
| 49 |
if self._name is None: |
| 50 |
self._name = name |
| 51 |
|
| 52 |
# See if a particular macOS architecture has been specified but only |
| 53 |
# for versions of Qt that support universal libraries. |
| 54 |
macos_thin_arch = self._get_macos_thin_arch(platform_tag) |
| 55 |
if qt_version < (5, 15, 10) or (6, 0, 0) <= qt_version < (6, 2, 0): |
| 56 |
macos_thin_arch = None |
| 57 |
|
| 58 |
if macos_thin_arch is not None: |
| 59 |
from .abstract_package import AbstractPackage |
| 60 |
|
| 61 |
if AbstractPackage.missing_executable('lipo'): |
| 62 |
raise UserException( |
| 63 |
"'lipo' from Xcode must be installed on your system") |
| 64 |
|
| 65 |
if macos_thin_arch is not None and AbstractPackage.missing_executable('codesign'): |
| 66 |
raise UserException( |
| 67 |
"'codesign' from Xcode must be installed on your system") |
| 68 |
# Bundle any sub-wheel files. |
| 69 |
if subwheel is True: |
| 70 |
for metadata_arch, subwheel_files in self._subwheel_files.items(): |
| 71 |
if self._is_platform(metadata_arch, platform_tag): |
| 72 |
# Bundle each file in the sub-wheel according to its type |
| 73 |
# (either 'data', 'exe', 'lib' or 'qtlib'). |
| 74 |
for file_type, file_name in subwheel_files: |
| 75 |
if file_type == 'data': |
| 76 |
self._bundle_file(file_name, target_qt_dir, qt_dir, |
| 77 |
platform_tag, macos_thin_arch, |
| 78 |
ignore_missing, might_be_code=False) |
| 79 |
|
| 80 |
elif file_type == 'exe': |
| 81 |
self._bundle_exe(file_name, target_qt_dir, qt_dir, |
| 82 |
qt_version, platform_tag, macos_thin_arch, |
| 83 |
ignore_missing) |
| 84 |
|
| 85 |
elif file_type == 'lib': |
| 86 |
self._bundle_library(file_name, target_qt_dir, |
| 87 |
qt_dir, platform_tag, macos_thin_arch, |
| 88 |
ignore_missing) |
| 89 |
|
| 90 |
elif file_type == 'qtlib': |
| 91 |
self._bundle_qt_library(file_name, target_qt_dir, |
| 92 |
qt_dir, qt_version, platform_tag, |
| 93 |
macos_thin_arch, ignore_missing, |
| 94 |
bundle_resources=False) |
| 95 |
|
| 96 |
# There is nothing else to do. |
| 97 |
return |
| 98 |
|
| 99 |
skip_files = [] |
| 100 |
|
| 101 |
# Build the list of files to skip as they will be in the sub-wheel. |
| 102 |
if subwheel is False: |
| 103 |
for metadata_arch, subwheel_files in self._subwheel_files.items(): |
| 104 |
if self._is_platform(metadata_arch, platform_tag): |
| 105 |
for file_type, file_name in subwheel_files: |
| 106 |
if file_type == 'qtlib': |
| 107 |
file_name = self._impl_from_library(file_name, |
| 108 |
platform_tag, qt_version) |
| 109 |
|
| 110 |
skip_files.append(file_name) |
| 111 |
|
| 112 |
# Bundle the Qt library that has been wrapped (if there is one). |
| 113 |
if self._dll: |
| 114 |
self._bundle_qt_library(self._name, target_qt_dir, qt_dir, |
| 115 |
qt_version, platform_tag, macos_thin_arch, ignore_missing, |
| 116 |
skip_files=skip_files) |
| 117 |
|
| 118 |
# Bundle any other dependent Qt libraries. |
| 119 |
for metadata_arch, libs in self._lib_deps.items(): |
| 120 |
if self._is_platform(metadata_arch, platform_tag): |
| 121 |
for lib in libs: |
| 122 |
self._bundle_qt_library(lib, target_qt_dir, qt_dir, |
| 123 |
qt_version, platform_tag, macos_thin_arch, |
| 124 |
ignore_missing, skip_files=skip_files) |
| 125 |
|
| 126 |
# Bundle any other libraries. |
| 127 |
lib_contents = None |
| 128 |
|
| 129 |
for metadata_arch, libs in self._other_lib_deps.items(): |
| 130 |
if self._is_platform(metadata_arch, platform_tag): |
| 131 |
for lib in libs: |
| 132 |
if '*' in lib: |
| 133 |
# A wildcard implies the dependency is optional. This |
| 134 |
# is mainly to (historically) deal with ICU on Windows. |
| 135 |
if lib_contents is None: |
| 136 |
lib_contents = os.listdir( |
| 137 |
self._get_qt_library_dir(qt_dir, |
| 138 |
platform_tag)) |
| 139 |
|
| 140 |
for qt_lib in lib_contents: |
| 141 |
if fnmatch.fnmatch(qt_lib, lib): |
| 142 |
self._bundle_library(qt_lib, target_qt_dir, |
| 143 |
qt_dir, platform_tag, macos_thin_arch, |
| 144 |
ignore_missing, skip_files=skip_files) |
| 145 |
else: |
| 146 |
self._bundle_library(lib, target_qt_dir, qt_dir, |
| 147 |
platform_tag, macos_thin_arch, ignore_missing, |
| 148 |
skip_files=skip_files) |
| 149 |
|
| 150 |
# Bundle any executables. |
| 151 |
for metadata_arch, exes in self._exes.items(): |
| 152 |
if self._is_platform(metadata_arch, platform_tag): |
| 153 |
for exe in exes: |
| 154 |
self._bundle_exe(exe, target_qt_dir, qt_dir, qt_version, |
| 155 |
platform_tag, macos_thin_arch, ignore_missing, |
| 156 |
skip_files=skip_files) |
| 157 |
|
| 158 |
# Bundle any QML files. |
| 159 |
qml_names = self._qml_names if self._qml_names is not None else [self._name] |
| 160 |
|
| 161 |
for qml_subdir in qml_names: |
| 162 |
self._bundle_nondebug(os.path.join('qml', qml_subdir), |
| 163 |
target_qt_dir, qt_dir, platform_tag, macos_thin_arch, |
| 164 |
ignore_missing, skip_files=skip_files) |
| 165 |
|
| 166 |
# Bundle any plugins. We haven't done the analysis of which plugins |
| 167 |
# belong to which package so we assume that only the QtCore package |
| 168 |
# will specify any to exclude and we bundle all of them with that. |
| 169 |
if self._excluded_plugins is not None: |
| 170 |
self._bundle_nondebug('plugins', target_qt_dir, qt_dir, |
| 171 |
platform_tag, macos_thin_arch, ignore_missing, |
| 172 |
skip_files=skip_files, exclude=self._excluded_plugins) |
| 173 |
|
| 174 |
# Bundle any translations: |
| 175 |
if self._translations: |
| 176 |
target_tr_dir = os.path.join(target_qt_dir, 'translations') |
| 177 |
tr_dir = os.path.join(qt_dir, 'translations') |
| 178 |
|
| 179 |
for qm in os.listdir(tr_dir): |
| 180 |
if qm.endswith('.qm'): |
| 181 |
for prefix in self._translations: |
| 182 |
if qm.startswith(prefix): |
| 183 |
self._bundle_file(qm, target_tr_dir, tr_dir, |
| 184 |
platform_tag, macos_thin_arch, |
| 185 |
ignore_missing, skip_files=skip_files, |
| 186 |
might_be_code=False) |
| 187 |
|
| 188 |
# Bundle any dynamically created files. |
| 189 |
for metadata_arch, files in self._files.items(): |
| 190 |
if self._is_platform(metadata_arch, platform_tag): |
| 191 |
for fn, content in files: |
| 192 |
fn = os.path.join(target_qt_dir, fn) |
| 193 |
os.makedirs(os.path.dirname(fn), exist_ok=True) |
| 194 |
|
| 195 |
with open(fn, 'w') as f: |
| 196 |
f.write(content) |
| 197 |
|
| 198 |
# Bundle anything else. |
| 199 |
for metadata_arch, others in self._others.items(): |
| 200 |
if self._is_platform(metadata_arch, platform_tag): |
| 201 |
for oth in others: |
| 202 |
self._bundle_file(oth, target_qt_dir, qt_dir, |
| 203 |
platform_tag, macos_thin_arch, ignore_missing, |
| 204 |
skip_files=skip_files, might_be_code=False) |
| 205 |
|
| 206 |
def is_applicable(self, qt_version): |
| 207 |
""" Returns True if this meta-data is applicable for a particular Qt |
| 208 |
version. |
| 209 |
""" |
| 210 |
|
| 211 |
return self._version is None or qt_version >= self._version |
| 212 |
|
| 213 |
@classmethod |
| 214 |
def _bundle_nondebug(cls, src_dir, target_qt_dir, qt_dir, platform_tag, |
| 215 |
macos_thin_arch, ignore_missing, skip_files=None, exclude=None): |
| 216 |
""" Bundle the non-debug contents of a directory. """ |
| 217 |
|
| 218 |
if exclude is None: |
| 219 |
exclude = () |
| 220 |
|
| 221 |
for dirpath, dirnames, filenames in os.walk(os.path.join(qt_dir, src_dir)): |
| 222 |
for ignore in exclude: |
| 223 |
try: |
| 224 |
dirnames.remove(ignore) |
| 225 |
except ValueError: |
| 226 |
pass |
| 227 |
|
| 228 |
for name in list(dirnames): |
| 229 |
if cls._is_debug(name, platform_tag): |
| 230 |
dirnames.remove(name) |
| 231 |
|
| 232 |
for name in filenames: |
| 233 |
if cls._is_debug(name, platform_tag): |
| 234 |
continue |
| 235 |
|
| 236 |
cls._bundle_file( |
| 237 |
os.path.relpath(os.path.join(dirpath, name), qt_dir), |
| 238 |
target_qt_dir, qt_dir, platform_tag, macos_thin_arch, |
| 239 |
ignore_missing, skip_files=skip_files) |
| 240 |
|
| 241 |
@classmethod |
| 242 |
def _bundle_exe(cls, name, target_qt_dir, qt_dir, qt_version, platform_tag, |
| 243 |
macos_thin_arch, ignore_missing, skip_files=None): |
| 244 |
""" Bundle an executable. """ |
| 245 |
|
| 246 |
exe = cls._bundle_file(name, target_qt_dir, qt_dir, platform_tag, |
| 247 |
macos_thin_arch, ignore_missing, skip_files=skip_files) |
| 248 |
|
| 249 |
if exe is not None: |
| 250 |
if cls._is_platform('linux', platform_tag): |
| 251 |
cls._fix_linux_executable(exe, qt_version) |
| 252 |
elif cls._is_platform('macos', platform_tag): |
| 253 |
cls._fix_macos_executable(exe, qt_version, macos_thin_arch) |
| 254 |
elif cls._is_platform('win', platform_tag): |
| 255 |
cls._fix_win_executable(exe) |
| 256 |
|
| 257 |
@staticmethod |
| 258 |
def _bundle_file(name, target_dir, src_dir, platform_tag, macos_thin_arch, |
| 259 |
ignore_missing, skip_files=None, ignore=None, might_be_code=True): |
| 260 |
""" Bundle a file (or directory) and return the name of the installed |
| 261 |
file (or directory) or None if it was missing. |
| 262 |
""" |
| 263 |
|
| 264 |
if skip_files is not None and name in skip_files: |
| 265 |
return None |
| 266 |
|
| 267 |
src = os.path.join(src_dir, name) |
| 268 |
dst = os.path.join(target_dir, name) |
| 269 |
|
| 270 |
os.makedirs(os.path.dirname(dst), exist_ok=True) |
| 271 |
|
| 272 |
if os.path.isdir(src): |
| 273 |
shutil.copytree(src, dst, ignore=ignore) |
| 274 |
elif os.path.isfile(src): |
| 275 |
if macos_thin_arch is not None and might_be_code: |
| 276 |
stderr = None if is_verbose() else subprocess.DEVNULL |
| 277 |
|
| 278 |
try: |
| 279 |
subprocess.run( |
| 280 |
['lipo', '-thin', macos_thin_arch, '-output', dst, |
| 281 |
src], |
| 282 |
stderr=stderr, check=True) |
| 283 |
except: |
| 284 |
# If there is any sort of error then just copy it. |
| 285 |
shutil.copy2(src, dst) |
| 286 |
|
| 287 |
subprocess.run(['codesign', '--force', '--sign', '-', dst], |
| 288 |
stderr=stderr, check=True) |
| 289 |
else: |
| 290 |
shutil.copy2(src, dst) |
| 291 |
elif ignore_missing: |
| 292 |
verbose("Ignoring missing '{0}'".format(name)) |
| 293 |
dst = None |
| 294 |
else: |
| 295 |
raise UserException( |
| 296 |
"'{0}' is missing from the Qt installation".format(name)) |
| 297 |
|
| 298 |
return dst |
| 299 |
|
| 300 |
@classmethod |
| 301 |
def _bundle_library(cls, name, target_qt_dir, qt_dir, platform_tag, |
| 302 |
macos_thin_arch, ignore_missing, skip_files=None, ignore=None): |
| 303 |
""" Bundle a library. """ |
| 304 |
|
| 305 |
cls._bundle_file(name, |
| 306 |
os.path.join(target_qt_dir, |
| 307 |
cls._get_qt_library_subdir(platform_tag)), |
| 308 |
cls._get_qt_library_dir(qt_dir, platform_tag), platform_tag, |
| 309 |
macos_thin_arch, ignore_missing, skip_files=skip_files, |
| 310 |
ignore=ignore) |
| 311 |
|
| 312 |
@classmethod |
| 313 |
def _bundle_qt_library(cls, name, target_qt_dir, qt_dir, qt_version, |
| 314 |
platform_tag, macos_thin_arch, ignore_missing, skip_files=None, |
| 315 |
bundle_resources=True): |
| 316 |
""" Bundle a Qt library. """ |
| 317 |
|
| 318 |
cls._bundle_library( |
| 319 |
cls._impl_from_library(name, platform_tag, qt_version), |
| 320 |
target_qt_dir, qt_dir, platform_tag, macos_thin_arch, |
| 321 |
ignore_missing, skip_files=skip_files) |
| 322 |
|
| 323 |
if bundle_resources and cls._is_platform('macos', platform_tag): |
| 324 |
# Copy the Resources directory without the unnecessary .prl files. |
| 325 |
cls._bundle_library('{}.framework/Resources'.format(name), |
| 326 |
target_qt_dir, qt_dir, platform_tag, macos_thin_arch, |
| 327 |
ignore_missing, skip_files=skip_files, |
| 328 |
ignore=lambda d, c: [f for f in c if f.endswith('.prl')]) |
| 329 |
|
| 330 |
@staticmethod |
| 331 |
def _create_qt_conf(exe): |
| 332 |
""" Create a qt.conf file for an executable. """ |
| 333 |
|
| 334 |
qt_conf = os.path.join(os.path.dirname(exe), 'qt.conf') |
| 335 |
|
| 336 |
with open(qt_conf, 'w') as f: |
| 337 |
f.write('[Paths]\nPrefix = ..\n') |
| 338 |
|
| 339 |
@classmethod |
| 340 |
def _fix_linux_executable(cls, exe, qt_version): |
| 341 |
""" Fix a Linux executable. """ |
| 342 |
|
| 343 |
# Note that this assumes the executable is QtWebEngineProcess. |
| 344 |
|
| 345 |
cls._create_qt_conf(exe) |
| 346 |
|
| 347 |
@classmethod |
| 348 |
def _fix_macos_executable(cls, exe, qt_version, macos_thin_arch): |
| 349 |
""" Fix a macOS executable. """ |
| 350 |
|
| 351 |
# Note that this assumes the executable is QtWebEngineProcess. |
| 352 |
|
| 353 |
# pip doesn't support symbolic links in wheels so the executable will |
| 354 |
# be installed in its 'logical' location so adjust rpath so that it can |
| 355 |
# still find the Qt libraries. The required change is simple so we |
| 356 |
# just patch the binary rather than require install_name_tool. Note |
| 357 |
# that install_name_tool is now always needed anyway. |
| 358 |
with open(exe, 'rb') as f: |
| 359 |
contents = f.read() |
| 360 |
|
| 361 |
contents = contents.replace(b'@loader_path/../../../../../../../', |
| 362 |
b'@loader_path/../../../../../\0\0\0\0\0\0') |
| 363 |
|
| 364 |
with open(exe, 'wb') as f: |
| 365 |
f.write(contents) |
| 366 |
|
| 367 |
if macos_thin_arch is not None: |
| 368 |
stderr = None if is_verbose() else subprocess.DEVNULL |
| 369 |
subprocess.run(['codesign', '--force', '--sign', '-', exe], |
| 370 |
stderr=stderr, check=True) |
| 371 |
|
| 372 |
@classmethod |
| 373 |
def _fix_win_executable(cls, exe): |
| 374 |
""" Fix a Windows executable. """ |
| 375 |
|
| 376 |
# Note that this assumes the executable is QtWebEngineProcess. |
| 377 |
|
| 378 |
cls._create_qt_conf(exe) |
| 379 |
|
| 380 |
@classmethod |
| 381 |
def _get_macos_thin_arch(cls, platform_tag): |
| 382 |
""" Return the single macOS architecture, or None if the platform is |
| 383 |
not macOS or is universal. |
| 384 |
""" |
| 385 |
|
| 386 |
if cls._is_platform('macos', platform_tag): |
| 387 |
if platform_tag.endswith('_intel') or platform_tag.endswith('_x86_64'): |
| 388 |
return 'x86_64' |
| 389 |
|
| 390 |
if platform_tag.endswith('_arm64'): |
| 391 |
return 'arm64' |
| 392 |
|
| 393 |
return None |
| 394 |
|
| 395 |
@classmethod |
| 396 |
def _get_qt_library_dir(cls, qt_dir, platform_tag): |
| 397 |
""" Return the name of the directory in the Qt installation containing |
| 398 |
the libraries. |
| 399 |
""" |
| 400 |
|
| 401 |
return os.path.join(qt_dir, cls._get_qt_library_subdir(platform_tag)) |
| 402 |
|
| 403 |
@classmethod |
| 404 |
def _get_qt_library_subdir(cls, platform_tag): |
| 405 |
""" Return the name of the sub-directory in the Qt installation |
| 406 |
containing the libraries. |
| 407 |
""" |
| 408 |
|
| 409 |
return 'bin' if cls._is_platform('win', platform_tag) else 'lib' |
| 410 |
|
| 411 |
@classmethod |
| 412 |
def _impl_from_library(cls, name, platform_tag, qt_version): |
| 413 |
""" Return the architecture-specific name of a Qt library. """ |
| 414 |
|
| 415 |
qt_major = qt_version[0] |
| 416 |
|
| 417 |
if cls._is_platform('linux', platform_tag): |
| 418 |
return 'libQt{}{}.so.{}'.format(qt_major, name[2:], qt_major) |
| 419 |
|
| 420 |
if cls._is_platform('macos', platform_tag): |
| 421 |
framework = '5' if qt_major == 5 else 'A' |
| 422 |
|
| 423 |
return '{}.framework/Versions/{}/{}'.format(name, framework, name) |
| 424 |
|
| 425 |
if cls._is_platform('win', platform_tag): |
| 426 |
return 'Qt{}{}.dll'.format(qt_major, name[2:]) |
| 427 |
|
| 428 |
@classmethod |
| 429 |
def _is_debug(cls, name, platform_tag): |
| 430 |
""" Return True if a name implies a debug version. """ |
| 431 |
|
| 432 |
if cls._is_platform('linux', platform_tag): |
| 433 |
return name.endswith('.debug') |
| 434 |
|
| 435 |
if cls._is_platform('macos', platform_tag): |
| 436 |
return name.endswith('_debug.dylib') or name.endswith('.dSYM') |
| 437 |
|
| 438 |
if cls._is_platform('win', platform_tag): |
| 439 |
# Special case known non-debug DLLs that end with a 'd'. |
| 440 |
if name.endswith('backend.dll'): |
| 441 |
return False |
| 442 |
|
| 443 |
return name.endswith('.pdb') or name.endswith('d.dll') |
| 444 |
|
| 445 |
@staticmethod |
| 446 |
def _is_platform(metadata_arch, platform_tag): |
| 447 |
""" Return True if a metadata archtitecture matches a platform tag. """ |
| 448 |
|
| 449 |
# See if it applies to all architectures. |
| 450 |
if metadata_arch == '': |
| 451 |
return True |
| 452 |
|
| 453 |
if metadata_arch == 'linux' and platform_tag.startswith('manylinux'): |
| 454 |
return True |
| 455 |
|
| 456 |
if metadata_arch == 'macos' and platform_tag.startswith('macosx'): |
| 457 |
return True |
| 458 |
|
| 459 |
if metadata_arch == 'win' and platform_tag.startswith('win'): |
| 460 |
return True |
| 461 |
|
| 462 |
return False |