package_release.sh 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 libvk_swiftshader.so vk_swiftshader_icd.json chrome-sandbox icudtl.dat v8_context_snapshot.bin \
  34. ./*.pak locales gnome-shell-extension "$pkg/"
  35. # CEF before 156 ships its own ANGLE libraries, newer builds link them into libcef.so
  36. for lib in libEGL.so libGLESv2.so; do
  37. if [ -e "$lib" ]; then
  38. cp -a "$lib" "$pkg/"
  39. fi
  40. done
  41. # programs of ours next to the engine: Chromium's child processes, and (builds with the KDE integration) the one that
  42. # unloads the engine's KWin scripts when it is killed
  43. helpers=(linux-wallpaperengine-web-helper)
  44. if [ -e linux-wallpaperengine-kwin-janitor ]; then
  45. cp -a linux-wallpaperengine-kwin-janitor "$pkg/"
  46. helpers+=(linux-wallpaperengine-kwin-janitor)
  47. fi
  48. # icon and hidden desktop entry, for packages to put on the system's paths
  49. mkdir -p "$pkg/share/applications"
  50. cp -a share/icons "$pkg/share/"
  51. cp -a share/applications/linux-wallpaperengine.desktop "$pkg/share/applications/"
  52. # GNUInstallDirs puts kissfft in lib64 on Fedora and lib elsewhere, the runpath covers both
  53. kissfft=(lib*/libkissfft-float.so*)
  54. if [ ! -e "${kissfft[0]}" ]; then
  55. echo "libkissfft-float.so not found in the install tree" >&2
  56. exit 1
  57. fi
  58. libdir="$(dirname "${kissfft[0]}")"
  59. [ "$portable" = 1 ] && libdir=lib
  60. mkdir -p "$pkg/$libdir"
  61. cp -a "${kissfft[@]}" "$pkg/$libdir/"
  62. # CEF ships libcef.so with debug symbols, 1.4 GB instead of about 220 MB
  63. strip --strip-unneeded "$pkg/linux-wallpaperengine" "${helpers[@]/#/$pkg/}" \
  64. "$pkg/liblinux-wallpaperengine-lib.so" "$pkg/libcef.so"
  65. if [ "$portable" = 1 ]; then
  66. # sonames (extended regexes) left to the system, everything else gets bundled
  67. system_libs=(
  68. # glibc and the compiler runtime
  69. 'ld-linux-x86-64' 'libc' 'libm' 'libdl' 'libpthread' 'librt' 'libresolv' 'libutil' 'libanl' 'libmvec'
  70. 'libBrokenLocale' 'libnsl' 'libstdc\+\+' 'libgcc_s'
  71. # GPU, display servers, input
  72. 'libGL' 'libGLX' 'libGLdispatch' 'libEGL' 'libGLESv1_CM' 'libGLESv2' 'libgbm' 'libdrm.*'
  73. 'libvulkan' 'libwayland-.*' 'libX11' 'libX11-xcb' 'libXau'
  74. 'libXdmcp' 'libXext' 'libXrender' 'libXfixes' 'libXrandr' 'libXi' 'libXcursor' 'libXinerama' 'libXdamage'
  75. 'libXcomposite' 'libXxf86vm' 'libxkbcommon.*' 'libxshmfence' 'libudev' 'libinput'
  76. 'libevdev'
  77. # xcb extensions Mesa and cairo use
  78. 'libxcb' 'libxcb-(dri2|dri3|present|sync|xfixes|randr|shm|glx|xkb|render|xinput|composite|damage)'
  79. # audio and IPC client libraries, they load plugins from system paths
  80. 'libdbus-1' 'libsystemd' 'libcap'
  81. # what CEF and the system's GTK stack link themselves
  82. 'libnss3' 'libnssutil3' 'libsmime3' 'libssl3' 'libnspr4' 'libplc4' 'libplds4' 'libcups' 'libatk-1\.0'
  83. 'libatk-bridge-2\.0' 'libatspi' 'libpango.*' 'libcairo.*' 'libpixman-1' 'libgdk_pixbuf-2\.0' 'libgtk-3' 'libgdk-3'
  84. 'libglib-2\.0' 'libgobject-2\.0' 'libgio-2\.0' 'libgmodule-2\.0' 'libgthread-2\.0' 'libffi' 'libpcre2-.*'
  85. 'libmount' 'libblkid' 'libselinux' 'libuuid' 'libz' 'libthai' 'libdatrie' 'libavahi-.*'
  86. )
  87. system_re="^($(IFS='|'; echo "${system_libs[*]}"))\.so(\..*)?$"
  88. # bundled but only used when the system lacks that soname, since system libraries may need their own newer
  89. # copy (GnuTLS and nettle). The launcher adds lib/fallback/<soname> for the missing ones
  90. fallback_libs=(
  91. 'libnettle' 'libhogweed' 'libgnutls' 'libtasn1' 'libidn2' 'libunistring' 'libp11-kit' 'libgmp' 'libgcrypt'
  92. 'libgpg-error' 'libssl' 'libcrypto' 'libbrotli(common|dec|enc)' 'liblzma' 'libzstd' 'liblz4' 'libbz2'
  93. 'libexpat' 'libpng16' 'libfreetype' 'libfontconfig' 'libharfbuzz' 'libgraphite2' 'libfribidi' 'libxml2'
  94. 'libkrb5.*' 'libk5crypto' 'libcom_err' 'libgssapi_krb5' 'libkeyutils' 'libSM' 'libICE' 'libOpenGL'
  95. # a bundled libva can't load newer system drivers; Ubuntu minimal may lack libva, but then has no drivers either
  96. 'libva' 'libva-(drm|x11|wayland)'
  97. # libmpv links PipeWire and ALSA; a desktop on PulseAudio only (openSUSE XFCE) has no libpipewire and the engine did not
  98. # start, a minimal Debian XFCE no libasound.
  99. # The system's copy wins where there is one, it loads the system's plugins
  100. 'libpipewire-0\.3' 'libasound'
  101. )
  102. fallback_re="^($(IFS='|'; echo "${fallback_libs[*]}"))\.so(\..*)?$"
  103. mkdir -p "$pkg/lib/fallback"
  104. elves=("$pkg/linux-wallpaperengine" "${helpers[@]/#/$pkg/}" "$pkg/liblinux-wallpaperengine-lib.so"
  105. "$pkg"/lib/libkissfft-float.so*)
  106. declare -A seen=()
  107. : >"$work/system"
  108. while [ "${#elves[@]}" -gt 0 ]; do
  109. elf="${elves[0]}"
  110. elves=("${elves[@]:1}")
  111. while read -r soname path; do
  112. [ -n "${seen[$soname]:-}" ] && continue
  113. seen[$soname]=1
  114. if [[ "$soname" =~ $system_re ]] || [ "$soname" = libcef.so ]; then
  115. echo "$soname" >>"$work/system"
  116. continue
  117. fi
  118. [ -e "$pkg/lib/$soname" ] || [[ "$path" == "$pkg"/* ]] && continue
  119. if [ "$path" = "not" ]; then
  120. echo "$elf needs $soname, which is not installed on the build system" >&2
  121. exit 1
  122. fi
  123. dest="$pkg/lib/$soname"
  124. if [[ "$soname" =~ $fallback_re ]]; then
  125. mkdir -p "$pkg/lib/fallback/$soname"
  126. dest="$pkg/lib/fallback/$soname/$soname"
  127. fi
  128. cp -L "$path" "$dest"
  129. chmod u+w "$dest"
  130. elves+=("$dest")
  131. done < <(LD_LIBRARY_PATH="$pkg:$pkg/lib:$(find "$pkg/lib/fallback" -mindepth 1 -type d | paste -sd:)" ldd "$elf" |
  132. awk '$2 == "=>" { print $1, $3 }')
  133. done
  134. mapfile -t bundled < <(find "$pkg/lib" -name '*.so*' -type f)
  135. command -v patchelf >/dev/null || { echo "--portable needs patchelf" >&2; exit 1; }
  136. # before patchelf, strip can break what patchelf moved around
  137. strip --strip-unneeded "${bundled[@]}" 2>/dev/null || true
  138. floor="${GLIBC_FLOOR:-2.35}"
  139. compat=liblwe-glibc-compat.so
  140. gcc -shared -fPIC -O2 -o "$pkg/lib/$compat" "$tools_dir/glibc_compat/lwe_glibc_compat.c"
  141. provided=$(nm -D --defined-only "$pkg/lib/$compat" | awk '$2 == "T" { print $3 }' | paste -sd,)
  142. python3 "$tools_dir/glibc_compat/retarget_glibc_versions.py" "$floor" "$provided" "${bundled[@]}" >/dev/null
  143. for f in "${bundled[@]}"; do
  144. if nm -D --undefined-only "$f" | awk '{ print $2 }' | sed 's/@.*//' | grep -qxF -f <(tr , '\n' <<<"$provided"); then
  145. patchelf --add-needed "$compat" "$f"
  146. fi
  147. done
  148. for f in "${bundled[@]}"; do
  149. case "$f" in
  150. "$pkg"/lib/fallback/*) patchelf --set-rpath '$ORIGIN/../..' "$f" ;;
  151. *) patchelf --set-rpath '$ORIGIN' "$f" ;;
  152. esac
  153. done
  154. for f in linux-wallpaperengine "${helpers[@]}" liblinux-wallpaperengine-lib.so; do
  155. patchelf --set-rpath '$ORIGIN:$ORIGIN/lib' "$pkg/$f"
  156. done
  157. sort -u "$work/system" | grep -v '^libcef.so$' >"$pkg/lib/SYSTEM_LIBS"
  158. mv "$pkg/linux-wallpaperengine" "$pkg/linux-wallpaperengine.bin"
  159. cp "$tools_dir/portable_launcher.sh" "$pkg/linux-wallpaperengine"
  160. chmod 755 "$pkg/linux-wallpaperengine"
  161. glibc=$(find "$pkg" -maxdepth 4 -type f \( -name '*.so*' -o -name '*.bin' -o -name '*-helper' \) \
  162. -exec objdump -T {} + 2>/dev/null | grep -o 'GLIBC_[0-9.]*' | sort -uV | tail -1 || true)
  163. echo "Bundled $(find "$pkg/lib" -name '*.so*' -type f | wc -l) libraries, needs $glibc"
  164. if [ "$(printf '%s\n' "${glibc#GLIBC_}" "$floor" | sort -V | tail -1)" != "$floor" ]; then
  165. echo "needs a newer glibc than $floor:" >&2
  166. find "$pkg" -maxdepth 4 -type f -name '*.so*' -exec sh -c \
  167. 'objdump -T "$1" | grep -q "($2)" && echo " $1"' _ {} "$glibc" \; >&2
  168. exit 1
  169. fi
  170. echo "${glibc#GLIBC_}" >"$pkg/lib/GLIBC_VERSION"
  171. fi
  172. mkdir -p "$out_dir"
  173. tar -czf "$out_dir/$name.tar.gz" -C "$work" "$name"
  174. echo "Wrote $out_dir/$name.tar.gz"