package_release.sh 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #!/usr/bin/env bash
  2. # Packs a finished build into a relocatable tarball: the binary, its library, the web helper, CEF's runtime files
  3. # and the shared kissfft, found through the $ORIGIN runpath `cmake --install` sets. The dependencies' own tools,
  4. # headers and static libs are left out.
  5. #
  6. # Usage:
  7. # tools/package_release.sh [--portable] <build-dir> <package-name> <output-dir>
  8. #
  9. # Writes <output-dir>/<package-name>.tar.gz with a single <package-name>/ folder inside.
  10. #
  11. # --portable also copies every shared library the binaries need into lib/, except system ones (glibc, GPU,
  12. # display and audio clients, D-Bus, the GTK/NSS stack CEF loads). Build it on the oldest glibc it should run on
  13. # (CI: Debian 12). Bundled libraries calling glibc functions newer than GLIBC_FLOOR (default 2.35) get them from
  14. # tools/glibc_compat. The engine binary becomes linux-wallpaperengine.bin behind tools/portable_launcher.sh, which
  15. # adds the lib/fallback copies the system lacks. lib/SYSTEM_LIBS lists the sonames left to the system,
  16. # tools/smoke_test_release.sh checks a tarball against a distribution.
  17. set -euo pipefail
  18. portable=0
  19. if [ "${1:-}" = --portable ]; then
  20. portable=1
  21. shift
  22. fi
  23. tools_dir="$(dirname "$(realpath "$0")")"
  24. build_dir="$(realpath "$1")"
  25. name="$2"
  26. out_dir="$(realpath "$3")"
  27. work="$(mktemp -d)"
  28. trap 'rm -rf "$work"' EXIT
  29. cmake --install "$build_dir" --prefix "$work/install" >/dev/null
  30. pkg="$work/$name"
  31. mkdir -p "$pkg"
  32. cd "$work/install"
  33. cp -a linux-wallpaperengine linux-wallpaperengine-web-helper liblinux-wallpaperengine-lib.so libcef.so libEGL.so \
  34. libGLESv2.so libvk_swiftshader.so vk_swiftshader_icd.json chrome-sandbox icudtl.dat v8_context_snapshot.bin \
  35. ./*.pak locales gnome-shell-extension "$pkg/"
  36. # icon and hidden desktop entry, for packages to put on the system's paths
  37. mkdir -p "$pkg/share/applications"
  38. cp -a share/icons "$pkg/share/"
  39. cp -a share/applications/linux-wallpaperengine.desktop "$pkg/share/applications/"
  40. # GNUInstallDirs puts kissfft in lib64 on Fedora and lib elsewhere, the runpath covers both
  41. kissfft=(lib*/libkissfft-float.so*)
  42. if [ ! -e "${kissfft[0]}" ]; then
  43. echo "libkissfft-float.so not found in the install tree" >&2
  44. exit 1
  45. fi
  46. libdir="$(dirname "${kissfft[0]}")"
  47. [ "$portable" = 1 ] && libdir=lib
  48. mkdir -p "$pkg/$libdir"
  49. cp -a "${kissfft[@]}" "$pkg/$libdir/"
  50. # CEF ships libcef.so with debug symbols, 1.4 GB instead of about 220 MB
  51. strip --strip-unneeded "$pkg/linux-wallpaperengine" "$pkg/linux-wallpaperengine-web-helper" \
  52. "$pkg/liblinux-wallpaperengine-lib.so" "$pkg/libcef.so"
  53. if [ "$portable" = 1 ]; then
  54. # sonames (extended regexes) left to the system, everything else gets bundled
  55. system_libs=(
  56. # glibc and the compiler runtime
  57. 'ld-linux-x86-64' 'libc' 'libm' 'libdl' 'libpthread' 'librt' 'libresolv' 'libutil' 'libanl' 'libmvec'
  58. 'libBrokenLocale' 'libnsl' 'libstdc\+\+' 'libgcc_s'
  59. # GPU, display servers, input
  60. 'libGL' 'libGLX' 'libGLdispatch' 'libOpenGL' 'libEGL' 'libGLESv1_CM' 'libGLESv2' 'libgbm' 'libdrm.*'
  61. # libva: an older bundled one can't load newer drivers
  62. 'libvulkan' 'libva' 'libva-(drm|x11|wayland)' 'libwayland-.*' 'libX11' 'libX11-xcb' 'libXau'
  63. 'libXdmcp' 'libXext' 'libXrender' 'libXfixes' 'libXrandr' 'libXi' 'libXcursor' 'libXinerama' 'libXdamage'
  64. 'libXcomposite' 'libXxf86vm' 'libxkbcommon.*' 'libxshmfence' 'libSM' 'libICE' 'libudev' 'libinput'
  65. 'libevdev'
  66. # xcb extensions Mesa and cairo use
  67. 'libxcb' 'libxcb-(dri2|dri3|present|sync|xfixes|randr|shm|glx|xkb|render|xinput|composite|damage)'
  68. # audio and IPC client libraries, they load plugins from system paths
  69. 'libasound' 'libpipewire-0\.3' 'libdbus-1' 'libsystemd' 'libcap'
  70. # what CEF and the system's GTK stack link themselves
  71. 'libnss3' 'libnssutil3' 'libsmime3' 'libssl3' 'libnspr4' 'libplc4' 'libplds4' 'libcups' 'libatk-1\.0'
  72. 'libatk-bridge-2\.0' 'libatspi' 'libpango.*' 'libcairo.*' 'libpixman-1' 'libgdk_pixbuf-2\.0' 'libgtk-3' 'libgdk-3'
  73. 'libglib-2\.0' 'libgobject-2\.0' 'libgio-2\.0' 'libgmodule-2\.0' 'libgthread-2\.0' 'libffi' 'libpcre2-.*'
  74. 'libmount' 'libblkid' 'libselinux' 'libuuid' 'libz' 'libthai' 'libdatrie' 'libavahi-.*'
  75. )
  76. system_re="^($(IFS='|'; echo "${system_libs[*]}"))\.so(\..*)?$"
  77. # bundled but only used when the system lacks that soname, since system libraries may need their own newer
  78. # copy (GnuTLS and nettle). The launcher adds lib/fallback/<soname> for the missing ones
  79. fallback_libs=(
  80. 'libnettle' 'libhogweed' 'libgnutls' 'libtasn1' 'libidn2' 'libunistring' 'libp11-kit' 'libgmp' 'libgcrypt'
  81. 'libgpg-error' 'libssl' 'libcrypto' 'libbrotli(common|dec|enc)' 'liblzma' 'libzstd' 'liblz4' 'libbz2'
  82. 'libexpat' 'libpng16' 'libfreetype' 'libfontconfig' 'libharfbuzz' 'libgraphite2' 'libfribidi' 'libxml2'
  83. 'libkrb5.*' 'libk5crypto' 'libcom_err' 'libgssapi_krb5' 'libkeyutils'
  84. )
  85. fallback_re="^($(IFS='|'; echo "${fallback_libs[*]}"))\.so(\..*)?$"
  86. mkdir -p "$pkg/lib/fallback"
  87. elves=("$pkg/linux-wallpaperengine" "$pkg/linux-wallpaperengine-web-helper" "$pkg/liblinux-wallpaperengine-lib.so"
  88. "$pkg"/lib/libkissfft-float.so*)
  89. declare -A seen=()
  90. : >"$work/system"
  91. while [ "${#elves[@]}" -gt 0 ]; do
  92. elf="${elves[0]}"
  93. elves=("${elves[@]:1}")
  94. while read -r soname path; do
  95. [ -n "${seen[$soname]:-}" ] && continue
  96. seen[$soname]=1
  97. if [[ "$soname" =~ $system_re ]] || [ "$soname" = libcef.so ]; then
  98. echo "$soname" >>"$work/system"
  99. continue
  100. fi
  101. [ -e "$pkg/lib/$soname" ] || [[ "$path" == "$pkg"/* ]] && continue
  102. if [ "$path" = "not" ]; then
  103. echo "$elf needs $soname, which is not installed on the build system" >&2
  104. exit 1
  105. fi
  106. dest="$pkg/lib/$soname"
  107. if [[ "$soname" =~ $fallback_re ]]; then
  108. mkdir -p "$pkg/lib/fallback/$soname"
  109. dest="$pkg/lib/fallback/$soname/$soname"
  110. fi
  111. cp -L "$path" "$dest"
  112. chmod u+w "$dest"
  113. elves+=("$dest")
  114. done < <(LD_LIBRARY_PATH="$pkg:$pkg/lib:$(find "$pkg/lib/fallback" -mindepth 1 -type d | paste -sd:)" ldd "$elf" |
  115. awk '$2 == "=>" { print $1, $3 }')
  116. done
  117. mapfile -t bundled < <(find "$pkg/lib" -name '*.so*' -type f)
  118. command -v patchelf >/dev/null || { echo "--portable needs patchelf" >&2; exit 1; }
  119. # before patchelf, strip can break what patchelf moved around
  120. strip --strip-unneeded "${bundled[@]}" 2>/dev/null || true
  121. floor="${GLIBC_FLOOR:-2.35}"
  122. compat=liblwe-glibc-compat.so
  123. gcc -shared -fPIC -O2 -o "$pkg/lib/$compat" "$tools_dir/glibc_compat/lwe_glibc_compat.c"
  124. provided=$(nm -D --defined-only "$pkg/lib/$compat" | awk '$2 == "T" { print $3 }' | paste -sd,)
  125. python3 "$tools_dir/glibc_compat/retarget_glibc_versions.py" "$floor" "$provided" "${bundled[@]}" >/dev/null
  126. for f in "${bundled[@]}"; do
  127. if nm -D --undefined-only "$f" | awk '{ print $2 }' | sed 's/@.*//' | grep -qxF -f <(tr , '\n' <<<"$provided"); then
  128. patchelf --add-needed "$compat" "$f"
  129. fi
  130. done
  131. for f in "${bundled[@]}"; do
  132. case "$f" in
  133. "$pkg"/lib/fallback/*) patchelf --set-rpath '$ORIGIN/../..' "$f" ;;
  134. *) patchelf --set-rpath '$ORIGIN' "$f" ;;
  135. esac
  136. done
  137. for f in linux-wallpaperengine linux-wallpaperengine-web-helper liblinux-wallpaperengine-lib.so; do
  138. patchelf --set-rpath '$ORIGIN:$ORIGIN/lib' "$pkg/$f"
  139. done
  140. sort -u "$work/system" | grep -v '^libcef.so$' >"$pkg/lib/SYSTEM_LIBS"
  141. mv "$pkg/linux-wallpaperengine" "$pkg/linux-wallpaperengine.bin"
  142. cp "$tools_dir/portable_launcher.sh" "$pkg/linux-wallpaperengine"
  143. chmod 755 "$pkg/linux-wallpaperengine"
  144. glibc=$(find "$pkg" -maxdepth 4 -type f \( -name '*.so*' -o -name '*.bin' -o -name '*-helper' \) \
  145. -exec objdump -T {} + 2>/dev/null | grep -o 'GLIBC_[0-9.]*' | sort -uV | tail -1 || true)
  146. echo "Bundled $(find "$pkg/lib" -name '*.so*' -type f | wc -l) libraries, needs $glibc"
  147. if [ "$(printf '%s\n' "${glibc#GLIBC_}" "$floor" | sort -V | tail -1)" != "$floor" ]; then
  148. echo "needs a newer glibc than $floor:" >&2
  149. find "$pkg" -maxdepth 4 -type f -name '*.so*' -exec sh -c \
  150. 'objdump -T "$1" | grep -q "($2)" && echo " $1"' _ {} "$glibc" \; >&2
  151. exit 1
  152. fi
  153. echo "${glibc#GLIBC_}" >"$pkg/lib/GLIBC_VERSION"
  154. fi
  155. mkdir -p "$out_dir"
  156. tar -czf "$out_dir/$name.tar.gz" -C "$work" "$name"
  157. echo "Wrote $out_dir/$name.tar.gz"