package_release.sh 8.9 KB

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