2022-08-03 15:52:16 +03:00
#!/usr/bin/env sh
# Safety measures
set -eu
2023-06-07 09:27:52 +03:00
repo = "https://github.com/simplex-chat/simplex-chat"
2022-08-03 15:52:16 +03:00
u = " $USER "
2023-06-07 09:27:52 +03:00
tmp = " $( mktemp -d -t) "
2022-12-08 11:55:37 +03:00
folder = " $tmp /simplex-chat "
2023-06-07 09:27:52 +03:00
2024-04-27 08:23:14 +00:00
nix_ver = "nix-2.22.0"
2023-06-07 09:27:52 +03:00
nix_url = " https://releases.nixos.org/nix/ $nix_ver /install "
2024-04-27 08:23:14 +00:00
nix_hash = "4fed7db867186c01ce2a2077da4a6950ed16232efbf78d0cd19700cff80559f9"
2023-06-07 09:27:52 +03:00
nix_config = " sandbox = true
max-jobs = auto
experimental-features = nix-command flakes"
2022-09-02 00:32:57 +03:00
commands = "nix git curl gradle zip unzip zipalign"
2023-06-07 09:27:52 +03:00
arches = " ${ ARCHES :- aarch64 armv7a } "
arch_map( ) {
case $1 in
aarch64) android_arch = "arm64-v8a" ; ;
armv7a) android_arch = "armeabi-v7a" ; ;
esac
}
2022-08-03 15:52:16 +03:00
2022-08-04 11:36:36 +03:00
nix_install( ) {
2022-08-03 15:52:16 +03:00
# Pre-setup nix
[ ! -d /nix ] && sudo sh -c " mkdir -p /nix && chown -R $u /nix "
# Install nix
2022-08-04 13:20:58 +03:00
curl -sSf " $nix_url " -o " $tmp /nix-install "
printf "%s %s" " $nix_hash " " $tmp /nix-install " | sha256sum -c
2022-08-03 15:52:16 +03:00
chmod +x " $tmp /nix-install " && " $tmp /nix-install " --no-daemon
. " $HOME /.nix-profile/etc/profile.d/nix.sh "
}
2022-08-04 11:36:36 +03:00
nix_setup( ) {
2023-06-07 09:27:52 +03:00
printf "%s" " $nix_config " > " $tmp /nix.conf "
2022-08-04 13:20:58 +03:00
export NIX_CONF_DIR = " $tmp / "
2022-08-04 11:36:36 +03:00
}
2022-08-03 15:52:16 +03:00
git_setup( ) {
2024-04-27 08:23:14 +00:00
if [ " $folder " != "." ] ; then
2023-07-18 23:42:58 +03:00
git clone " $repo " " $folder "
2024-04-27 08:23:14 +00:00
fi
2023-03-25 21:51:27 +00:00
2024-04-27 08:23:14 +00:00
if [ -z ${ git_skip +x } ] ; then
git -C " $folder " checkout " $commit "
fi
2022-08-03 15:52:16 +03:00
}
checks( ) {
set +u
2022-08-05 10:14:32 +03:00
2022-08-04 11:36:36 +03:00
for i in $commands ; do
case $i in
nix)
if ! command -v " $i " > /dev/null 2>& 1 || [ ! -f " $HOME /.nix-profile/etc/profile.d/nix.sh " ] ; then
nix_install
fi
nix_setup
; ;
2023-07-18 23:42:58 +03:00
gradle)
if ! command -v " $i " > /dev/null 2>& 1; then
commands_failed = " $i $commands_failed "
else
gradle_ver_local = " $( gradle -v | grep Gradle | awk '{print $2}' ) "
gradle_ver_local_compare = " $( printf ${ gradle_ver_local :- 0 .0 } | awk -F. '{print $1$2}' ) "
gradle_ver_remote = " $( grep distributionUrl ${ folder } /apps/multiplatform/gradle/wrapper/gradle-wrapper.properties) "
gradle_ver_remote = " ${ gradle_ver_remote #*- } "
gradle_ver_remote = " ${ gradle_ver_remote %-* } "
gradle_ver_remote_compare = " $( printf ${ gradle_ver_remote } | awk -F. '{print $1$2}' ) "
if [ " $gradle_ver_local_compare " != " $gradle_ver_remote_compare " ] ; then
commands_failed = " $i [installed= ${ gradle_ver_local } ,required= ${ gradle_ver_remote } ] $commands_failed "
fi
fi
; ;
2022-08-04 11:36:36 +03:00
*)
if ! command -v " $i " > /dev/null 2>& 1; then
commands_failed = " $i $commands_failed "
fi
; ;
esac
done
2022-08-03 15:52:16 +03:00
if [ -n " $commands_failed " ] ; then
commands_failed = ${ commands_failed % * }
printf "%s is not found in your \$PATH. Please install them and re-run the script.\n" " $commands_failed "
exit 1
fi
set -u
}
build( ) {
2023-06-07 09:27:52 +03:00
# Build preparations
2023-06-29 14:53:11 +03:00
sed -i.bak 's/${extract_native_libs}/true/' " $folder /apps/multiplatform/android/src/main/AndroidManifest.xml "
2023-07-18 23:42:58 +03:00
sed -i.bak 's/jniLibs.useLegacyPackaging =.*/jniLibs.useLegacyPackaging = true/' " $folder /apps/multiplatform/android/build.gradle.kts "
sed -i.bak '/android {/a lint {abortOnError = false}' " $folder /apps/multiplatform/android/build.gradle.kts "
2024-04-27 08:23:14 +00:00
sed -i.bak '/tasks/Q' " $folder /apps/multiplatform/android/build.gradle.kts "
2022-09-02 00:32:57 +03:00
2023-06-07 09:27:52 +03:00
for arch in $arches ; do
2024-04-27 08:23:14 +00:00
if [ " $arch " = "armv7a" ] ; then
2024-01-02 17:39:23 +03:00
android_simplex_lib = " ${ folder } #hydraJobs. ${ arch } -android:lib:simplex-chat.x86_64-linux "
android_support_lib = " ${ folder } #hydraJobs. ${ arch } -android:lib:support.x86_64-linux "
else
android_simplex_lib = " ${ folder } #hydraJobs.x86_64-linux. ${ arch } -android:lib:simplex-chat "
android_support_lib = " ${ folder } #hydraJobs.x86_64-linux. ${ arch } -android:lib:support "
2023-12-30 13:19:56 +03:00
fi
2023-06-07 09:27:52 +03:00
android_simplex_lib_output = " ${ PWD } /result/pkg- ${ arch } -android-libsimplex.zip "
android_support_lib_output = " ${ PWD } /result/pkg- ${ arch } -android-libsupport.zip "
2023-06-29 14:53:11 +03:00
2023-06-07 09:27:52 +03:00
arch_map " $arch "
android_tmp_folder = " ${ tmp } /android- ${ arch } "
2023-06-29 14:53:11 +03:00
android_apk_output = " ${ folder } /apps/multiplatform/android/build/outputs/apk/release/android- ${ android_arch } -release-unsigned.apk "
2023-06-07 09:27:52 +03:00
android_apk_output_final = " simplex-chat- ${ android_arch } .apk "
2023-07-18 23:42:58 +03:00
libs_folder = " ${ folder } /apps/multiplatform/common/src/commonMain/cpp/android/libs "
2023-06-07 09:27:52 +03:00
# Create missing folders
mkdir -p " $libs_folder / $android_arch "
nix build " $android_simplex_lib "
unzip -o " $android_simplex_lib_output " -d " $libs_folder / $android_arch "
nix build " $android_support_lib "
unzip -o " $android_support_lib_output " -d " $libs_folder / $android_arch "
# Build only one arch
2023-07-18 23:42:58 +03:00
sed -i.bak " s/include(.*/include(\" ${ android_arch } \")/ " " $folder /apps/multiplatform/android/build.gradle.kts "
gradle -p " $folder /apps/multiplatform/ " clean :android:assembleRelease
2023-06-29 14:53:11 +03:00
2023-06-07 09:27:52 +03:00
mkdir -p " $android_tmp_folder "
unzip -oqd " $android_tmp_folder " " $android_apk_output "
2023-06-29 14:53:11 +03:00
2023-06-07 09:27:52 +03:00
(
cd " $android_tmp_folder " && \
zip -rq5 " $tmp / $android_apk_output_final " . && \
zip -rq0 " $tmp / $android_apk_output_final " resources.arsc res
)
zipalign -p -f 4 " $tmp / $android_apk_output_final " " $PWD / $android_apk_output_final "
rm -rf " $libs_folder / $android_arch "
done
2022-08-03 15:52:16 +03:00
}
final( ) {
2023-06-07 09:27:52 +03:00
printf 'Simplex-chat was successfully compiled: %s/simplex-chat-*.apk\nDelete nix and gradle caches with "rm -rf /nix && rm $HOME/.nix* && $HOME/.gradle/caches" in case if no longer needed.\n' " $PWD "
2022-08-03 15:52:16 +03:00
}
2024-04-27 08:23:14 +00:00
pre( ) {
while getopts ":sg" opt; do
2022-12-08 11:55:37 +03:00
case $opt in
s) folder = "." ; ;
2024-04-27 08:23:14 +00:00
g) git_skip = 1 ; ;
2022-12-08 11:55:37 +03:00
*) printf "Flag '-%s' doesn't exist.\n" " $OPTARG " ; exit 1 ; ;
esac
done
2024-04-27 08:23:14 +00:00
2022-12-08 11:55:37 +03:00
shift $(( $OPTIND - 1 ))
2024-04-27 08:23:14 +00:00
commit = " ${ 1 :- HEAD } "
}
main( ) {
pre " $@ "
2022-08-03 15:52:16 +03:00
git_setup
2023-07-18 23:42:58 +03:00
checks
2022-08-03 15:52:16 +03:00
build
final
}
2022-12-08 11:55:37 +03:00
main " $@ "