Skip to content

Instantly share code, notes, and snippets.

@azubieta
Created March 2, 2022 18:43
Show Gist options
  • Save azubieta/cffda1c1ae0f18f00822d78165f3d815 to your computer and use it in GitHub Desktop.
Save azubieta/cffda1c1ae0f18f00822d78165f3d815 to your computer and use it in GitHub Desktop.
Bash implementation of the appimage-builder AppRun
#!/bin/bash
export ORIGIN="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
PRINTENV_BIN=$(which printenv)
set_bundle_env() {
eval "echo SETTING_ENV: $NAME $VALUE"
NAME="$1"
VALUE="$2"
STARTUP_VAL="$VALUE"
ORIGINAL_VAL="$($PRINTENV_BIN $NAME)"
eval "export APPRUN_ORIGINAL_$NAME=$ORIGINAL_VAL"
eval "export APPRUN_STARTUP_$NAME=$STARTUP_VAL"
eval "export $NAME=$VALUE"
}
read_ld_so_version() {
VERSION=$(strings "$1" | grep -Eo "ld-[0-9]{1,4}\.[0-9]{1,4}.so" | cut -f 2 -d- | rev | cut -f 2- -d . | rev )
echo "$VERSION"
}
format_exported_path() {
EXPORTED_PATH="/tmp/appimage-$APPIMAGE_UUID-$(basename $1)"
echo "$EXPORTED_PATH"
}
input="$ORIGIN/.env"
while IFS= read -r line
do
NAME="${line%=*}"
VALUE="${line#*=}"
case $NAME in
EXEC_PATH | EXEC_ARGS | APPDIR_LIBRARY_PATH | APPDIR_LIBC_VERSION | LIBC_LIBRARY_PATH | SYSTEM_INTERP | EXPORTED_BINARIES)
eval "$line"
;;
APPDIR | ORIGIN | APPIMAGE_UUID)
set_bundle_env "$NAME" "$VALUE"
;;
esac
done < "$input"
echo ""
echo --- Exporting Binaries ---
readarray -d : -t strarr <<< "$EXPORTED_BINARIES"
for (( n=0; n < ${#strarr[*]}; n++))
do
ORIG_PATH="${strarr[n]}"
EXPORTED_PATH="$(format_exported_path $ORIG_PATH)"
cp -v $ORIG_PATH $EXPORTED_PATH
done
echo ""
echo --- Setting Runtime ---
INTERPRETER_PATH="${SYSTEM_INTERP%;*}"
LD_SO_VERSION=$(read_ld_so_version "$INTERPRETER_PATH")
echo SYSTEM_INTERPRETER version: $LD_SO_VERSION path: "$INTERPRETER_PATH"
BUNDLE_INTERPRETER_PATH="$APPDIR/opt/libc/${SYSTEM_INTERP%;*}"
BUNDLE_LD_SO_VERSION=$(read_ld_so_version "$BUNDLE_INTERPRETER_PATH")
echo BUNDLE_INTERPRETER version: $BUNDLE_LD_SO_VERSION path: "$BUNDLE_INTERPRETER_PATH"
MAJOR_VERSION=$(printf "$BUNDLE_LD_SO_VERSION\n$LD_SO_VERSION\n" | sort -V | tail -1)
echo MAJOR_VERSION: $MAJOR_VERSION
EXPORTED_LD_SO_PATH="$(format_exported_path $BUNDLE_INTERPRETER_PATH)"
if [ "$MAJOR_VERSION" == "$BUNDLE_LD_SO_VERSION" ]; then
echo ""
echo " ## USING BUNDLE RUNTIME ##"
cp $BUNDLE_INTERPRETER_PATH $EXPORTED_LD_SO_PATH
set_bundle_env "LD_LIBRARY_PATH" "$LIBC_LIBRARY_PATH:$APPDIR_LIBRARY_PATH"
else
echo ""
echo " ## USING SYSTEM RUNTIME ##"
cp $INTERPRETER_PATH $EXPORTED_LD_SO_PATH
set_bundle_env "LD_LIBRARY_PATH" "$APPDIR_LIBRARY_PATH"
fi
while IFS= read -r line
do
NAME="${line%=*}"
VALUE="${line#*=}"
case $NAME in
APPDIR_LIBRARY_PATH | APPDIR_LIBC_VERSION | LIBC_LIBRARY_PATH | SYSTEM_INTERP | EXPORTED_BINARIES )
unset $NAME
;;
EXEC_PATH | EXEC_ARGS | APPDIR | ORIGIN )
;;
*)
set_bundle_env "$NAME" "$VALUE"
;;
esac
done < "$input"
echo ""
echo --- Launching Application ---
EXEC_LINE="exec $EXEC_PATH $EXEC_ARGS $@"
echo "$EXEC_LINE"
eval "$EXEC_LINE"
@azubieta
Copy link
Author

azubieta commented Mar 2, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment