File create_tarball.sh of Package nodejs-signal-ringrtc

#!/bin/bash
# shellcheck disable=2181

SIGNAL_PKGVERSION=$(grep '%define webrtc_req_version' *.spec | sed -e 's/^.*version *//')
SIGNAL_PKGNAME="webrtc"
SIGNAL_PKGDIR="$(pwd)"
SIGNAL_TMPDIR=$(mktemp --tmpdir -d signal-webrtc-XXXXXXXX)
SIGNAL_PATH="$SIGNAL_TMPDIR/$SIGNAL_PKGNAME-$SIGNAL_PKGVERSION"

echo "NAME:    $SIGNAL_PKGNAME"
echo "VERSION: $SIGNAL_PKGVERSION"
echo "PATH:    $SIGNAL_PATH"

echo -n "This script will download about 10G to $SIGNAL_TMPDIR, continue? "
read $ans

cleanup_tmpdir() {
    popd 2> /dev/null || true
    rm -rf "$SIGNAL_TMPDIR"
}
trap cleanup_tmpdir SIGINT

cleanup_and_exit() {
    cleanup_tmpdir
    if test "$1" = 0 -o -z "$1"; then
        exit 0
    else
        exit "$1"
    fi
}

pushd "$SIGNAL_TMPDIR" || cleanup_and_exit 1

echo ">>>>>> Get depot tools"
git clone --depth=1 https://chromium.googlesource.com/chromium/tools/depot_tools.git
PATH="$(pwd)/depot_tools:$PATH"
export PATH

# HACK to make gclient much faster, do not download entire history
sed -i "s/, '--progress']$/, '--progress', '--filter=tree:0']/" depot_tools/gclient_scm.py
# googlesource these days likes to error out on tree:0 but it should not be needed
sed -i 's/remote or self.remote,$/remote or self.remote, "--depth=1"/' depot_tools/gclient_scm.py

# HACK I want to see progress from git checkout to ensure it does not hang
sed -i 's/if quiet is None:$/if True:/' depot_tools/gclient_scm.py


echo ">>>>>> Create gclient config"
cat > .gclient << EOF
solutions = [
  {
    "name"        : "src",
    "url"         : "https://github.com/signalapp/webrtc.git",
    "deps_file"   : "DEPS",
  },
]
EOF

echo ">>>>>> Run gclient sync"
gclient sync -v --jobs $(nproc) --nohooks --no-history --shallow "--revision=src@${SIGNAL_PKGVERSION}"
if [ $? -ne 0 ]; then
    echo "ERROR: gclient sync failed"
    cleanup_and_exit 1
fi

mv src "$SIGNAL_PKGNAME-$SIGNAL_PKGVERSION"

pushd "$SIGNAL_PATH" || cleanup_and_exit 1

echo ">>>>>> Create LASTCHANGE(.committime) file"
echo -n "LASTCHANGE=$(git log -1 --format=format:%H HEAD)" > build/util/LASTCHANGE
# shellcheck disable=1091
source build/util/LASTCHANGE
echo -n "$(git log -1 --date=unix --format=format:%cd "$LASTCHANGE")" > build/util/LASTCHANGE.committime

echo ">>>>>> Remove bundled libs"
keeplibs=(
    common_audio/third_party/ooura
    common_audio/third_party/spl_sqrt_floor
    modules/third_party/fft
    modules/third_party/g711
    modules/third_party/g722
    rtc_base/third_party/base64
    rtc_base/third_party/sigslot
    third_party/abseil-cpp #Leap is unusable (no C++17)
    third_party/boringssl
    third_party/boringssl/src/third_party/fiat
    third_party/libsrtp #Needs to be built against boringssl, not openssl
    third_party/libyuv #Fedora is old
    third_party/perfetto
    third_party/pffft
    third_party/rnnoise
)
build/linux/unbundle/remove_bundled_libraries.py "${keeplibs[@]}" --do-remove

#remove vendored opus copy
find ringrtc/opus -type f ! -name '*.gn' -a ! -name '*.gni' -a ! -name '*.gyp' -a ! -name '*.gypi' -delete

#remove some more unneeded heavy files
rm -rf data
rm -rf tools/disable_tests
rm -rf tools/perf/{page_sets,testdata}

find . -type d -name .git -print0 | xargs -0 rm -rf
# Remove generatted python bytecode
find . -type d -name __pycache__ -print0 | xargs -0 rm -rvf
find . -type f -name '*.pyc' -print -delete

echo ">>>>>> Remove non-free binaries"
find . -type f -name "*.wasm" -print -delete
find . -type f -name "*.jar" -print -delete
find . -type f -name "*.exe" -print -delete
find . -type f -name "*.node" -print -delete
find . -type f -name "*.dll" -print -delete
find . -type f -name "*.dylib" -print -delete
find . -type f -name "*.so" -print -delete
find . -type f -name "*.o" -print -delete
find . -type f -name "*.a" -print -delete

#We use sponge to avoid a race condition between find and rm
find -type f | sponge | xargs -P$(nproc) -- sh -c 'file -S "$@" | grep -v '\'' .*script'\'' | grep '\'' .*executable'\'' | tee /dev/stderr | sed '\''s/: .*//'\'' | xargs rm -fv'

# Remove empty directories
find . -type d -empty -print -delete
popd || cleanup_and_exit 1

#echo '>>>>>> Hardlink duplicate files to reduce extraction time'
#Disabled this — it fails to link sometimes causing tarball nondeterminism
#/usr/lib/rpm/fdupes_wrapper node_modules

echo ">>>>>> Create tarball"
ZSTD_CLEVEL=19 ZSTD_NBTHREADS=$(nproc) tar --zstd --sort=name -vvScf "${SIGNAL_PKGDIR}/${SIGNAL_PKGNAME}-${SIGNAL_PKGVERSION}.tar.zst" "${SIGNAL_PKGNAME}-${SIGNAL_PKGVERSION}"
if [ $? -ne 0 ]; then
    cleanup_and_exit 1
fi

popd || cleanup_and_exit 1

cleanup_and_exit 0
openSUSE Build Service is sponsored by