#!/usr/bin/env python3 """ Populates a Linux AppDir for asteria-qt (x86_64 or arm64/aarch64) by copying required Qt plugins, QML modules, and transitive ELF shared libraries using architecture-independent tools (readelf + patchelf). Usage: bundle-linux-appdir.py --appdir --qt-dir [--arch {x86_64,arm64}] """ import argparse import os import re import shutil import stat import subprocess import sys # Standard AppImage host-provided system/driver libraries (excluded from AppDir/usr/lib) EXCLUDED_SYSTEM_LIBS = { "ld-linux-x86-64.so.2", "ld-linux-aarch64.so.1", "libanl.so.1", "libc.so.6", "libcrypt.so.1", "libdbus-1.so.3", "libdl.so.2", "libdrm.so.2", "libEGL.so.1", "libexpat.so.1", "libfontconfig.so.1", "libfreetype.so.6", "libgbm.so.1", "libgcc_s.so.1", "libGL.so.1", "libGLdispatch.so.0", "libglib-2.0.so.0", "libGLX.so.0", "libharfbuzz.so.0", "libICE.so.6", "libm.so.6", "libnsl.so.1", "libOpenGL.so.0", "libpthread.so.0", "libresolv.so.2", "librt.so.1", "libSM.so.6", "libstdc++.so.6", "libutil.so.1", "libvulkan.so.1", "libwayland-client.so.0", "libwayland-cursor.so.0", "libwayland-egl.so.1", "libX11.so.6", "libX11-xcb.so.1", "libxcb.so.1", "libXext.so.6", "libz.so.1", } PLUGIN_DIRS = [ "platforms", "wayland-decoration-client", "wayland-graphics-integration-client", "wayland-shell-integration", "xcbglintegrations", "platformthemes", "platforminputcontexts", "imageformats", "iconengines", "tls", ] # Skip optional plugins that pull in heavy/unrelated desktop or embedded stacks SKIP_PLUGINS = { "libqeglfs.so", "libqlinuxfb.so", "libqvkkhrdisplay.so", "libqvnc.so", "libqgtk3.so", "libqcertonlybackend.so", } QML_MODULES = [ "QtCore", "Qt", "QtQml", "QtQuick", ] UNUSED_QTQUICK_SUBDIRS = { "tooling", "LocalStorage", "Particles", "Pdf", "Scene2D", "Scene3D", "Timeline", "VirtualKeyboard", } NEEDED_RE = re.compile(r"\(NEEDED\)\s+Shared library:\s+\[([^\]]+)\]") def is_elf(filepath): if not os.path.isfile(filepath) or os.path.islink(filepath): return False try: with open(filepath, "rb") as f: return f.read(4) == b"\x7fELF" except OSError: return False def get_dt_needed(filepath): res = subprocess.run(["readelf", "-d", filepath], capture_output=True, text=True) if res.returncode != 0: return [] return NEEDED_RE.findall(res.stdout) def setup_appdir_metadata(appdir): desktop_src = os.path.join(appdir, "usr", "share", "applications", "asteria-qt.desktop") icon_src = os.path.join(appdir, "usr", "share", "icons", "hicolor", "256x256", "apps", "asteria-qt.png") if os.path.isfile(desktop_src): shutil.copy2(desktop_src, os.path.join(appdir, "asteria-qt.desktop")) if os.path.isfile(icon_src): shutil.copy2(icon_src, os.path.join(appdir, "asteria-qt.png")) dir_icon = os.path.join(appdir, ".DirIcon") if os.path.lexists(dir_icon): os.remove(dir_icon) os.symlink("asteria-qt.png", dir_icon) apprun_path = os.path.join(appdir, "AppRun") with open(apprun_path, "w", encoding="utf-8") as f: f.write( "#!/bin/sh\n" 'HERE="$(dirname "$(readlink -f "$0")")"\n' 'export LD_LIBRARY_PATH="$HERE/usr/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"\n' 'export QT_PLUGIN_PATH="$HERE/usr/plugins"\n' 'export QML2_IMPORT_PATH="$HERE/usr/qml"\n' 'export QML_IMPORT_PATH="$HERE/usr/qml"\n' 'exec "$HERE/usr/bin/asteria-qt" "$@"\n' ) os.chmod(apprun_path, 0o755) qt_conf_path = os.path.join(appdir, "usr", "bin", "qt.conf") with open(qt_conf_path, "w", encoding="utf-8") as f: f.write("[Paths]\nPrefix = ..\nPlugins = plugins\nImports = qml\nQml2Imports = qml\n") def copy_qt_plugins_and_qml(appdir, qt_dir): plugins_src = os.path.join(qt_dir, "plugins") plugins_dst = os.path.join(appdir, "usr", "plugins") os.makedirs(plugins_dst, exist_ok=True) for pdir in PLUGIN_DIRS: src_sub = os.path.join(plugins_src, pdir) if not os.path.isdir(src_sub): continue dst_sub = os.path.join(plugins_dst, pdir) os.makedirs(dst_sub, exist_ok=True) for fname in os.listdir(src_sub): if not fname.endswith(".so") or fname in SKIP_PLUGINS: continue shutil.copy2(os.path.join(src_sub, fname), os.path.join(dst_sub, fname)) qml_src = os.path.join(qt_dir, "qml") qml_dst = os.path.join(appdir, "usr", "qml") os.makedirs(qml_dst, exist_ok=True) for mod in QML_MODULES: src_mod = os.path.join(qml_src, mod) if os.path.isdir(src_mod): dst_mod = os.path.join(qml_dst, mod) if os.path.exists(dst_mod): shutil.rmtree(dst_mod) shutil.copytree(src_mod, dst_mod) for unused in UNUSED_QTQUICK_SUBDIRS: unused_path = os.path.join(qml_dst, "QtQuick", unused) if os.path.isdir(unused_path): shutil.rmtree(unused_path) for root, _, files in os.walk(qml_dst): for fname in files: if fname.endswith((".qmltypes", ".metainfo", ".prl")): os.remove(os.path.join(root, fname)) def find_library(soname, search_dirs): for sdir in search_dirs: candidate = os.path.join(sdir, soname) if os.path.exists(candidate): return os.path.realpath(candidate) return None def bundle_and_verify_libs(appdir, qt_dir, triplet): lib_dst = os.path.join(appdir, "usr", "lib") os.makedirs(lib_dst, exist_ok=True) search_dirs = [ os.path.join(qt_dir, "lib"), f"/usr/lib/{triplet}", f"/lib/{triplet}", ] # Remove optional plugins (like imageformats/tls) whose third-party backing lib isn't installed plugins_dst = os.path.join(appdir, "usr", "plugins") for root, _, files in os.walk(plugins_dst): for fname in list(files): fpath = os.path.join(root, fname) if not is_elf(fpath): continue needed = get_dt_needed(fpath) missing_opt = [ n for n in needed if n not in EXCLUDED_SYSTEM_LIBS and find_library(n, search_dirs) is None ] if missing_opt and os.path.basename(root) in ("imageformats", "tls", "platforminputcontexts", "wayland-graphics-integration-client"): print(f" Skipping optional plugin {fname} (missing optional system lib: {', '.join(missing_opt)})") os.remove(fpath) # Collect initial ELF files in AppDir queue = [] for root, _, files in os.walk(appdir): for fname in files: fpath = os.path.join(root, fname) if is_elf(fpath): queue.append(fpath) bundled_sonames = set(os.listdir(lib_dst)) copied_count = 0 while queue: elf_path = queue.pop(0) for soname in get_dt_needed(elf_path): if soname in EXCLUDED_SYSTEM_LIBS or soname in bundled_sonames: continue real_src = find_library(soname, search_dirs) if real_src is None: continue dst_file = os.path.join(lib_dst, soname) shutil.copy2(real_src, dst_file) os.chmod(dst_file, stat.S_IMODE(os.stat(dst_file).st_mode) | stat.S_IWUSR) bundled_sonames.add(soname) queue.append(dst_file) copied_count += 1 print(f"==> Bundled {copied_count} shared libraries into {lib_dst}") # Patch RPATH so executable and libraries resolve $ORIGIN exe_path = os.path.join(appdir, "usr", "bin", "asteria-qt") subprocess.run(["patchelf", "--set-rpath", "$ORIGIN/../lib", exe_path], check=True) for fname in os.listdir(lib_dst): fpath = os.path.join(lib_dst, fname) if is_elf(fpath): subprocess.run(["patchelf", "--set-rpath", "$ORIGIN", fpath], check=False) # Verify all DT_NEEDED dependencies in AppDir missing_by_soname = {} all_elfs = [] for root, _, files in os.walk(appdir): for fname in files: fpath = os.path.join(root, fname) if is_elf(fpath): all_elfs.append(fpath) for elf_path in sorted(all_elfs): rel = os.path.relpath(elf_path, appdir) for soname in get_dt_needed(elf_path): if soname in EXCLUDED_SYSTEM_LIBS or soname in bundled_sonames: continue missing_by_soname.setdefault(soname, []).append(rel) if missing_by_soname: print(f"FAILED: Found {len(missing_by_soname)} missing ELF shared libraries in {appdir}:", file=sys.stderr) for soname, importers in sorted(missing_by_soname.items()): print(f" [MISSING] {soname} needed by:", file=sys.stderr) for imp in importers[:5]: print(f" - {imp}", file=sys.stderr) sys.exit(1) print(f"==> ELF verification passed: scanned {len(all_elfs)} binaries in {appdir}, 0 missing dependencies.") def main(): parser = argparse.ArgumentParser(description="Bundle Qt plugins, QML modules, and ELF libraries into AppDir") parser.add_argument("--appdir", required=True, help="Path to AppDir") parser.add_argument("--qt-dir", required=True, help="Path to target Qt installation directory") parser.add_argument("--arch", default="x86_64", choices=["x86_64", "arm64", "aarch64"]) args = parser.parse_args() triplet = "aarch64-linux-gnu" if args.arch in ("arm64", "aarch64") else "x86_64-linux-gnu" setup_appdir_metadata(args.appdir) copy_qt_plugins_and_qml(args.appdir, args.qt_dir) bundle_and_verify_libs(args.appdir, args.qt_dir, triplet) if __name__ == "__main__": main()