xref: /aosp_15_r20/external/pytorch/scripts/build_android_gradle.sh (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1*da0073e9SAndroid Build Coastguard Worker#!/usr/bin/env bash
2*da0073e9SAndroid Build Coastguard Workerset -eux -o pipefail
3*da0073e9SAndroid Build Coastguard Worker
4*da0073e9SAndroid Build Coastguard Workerenv
5*da0073e9SAndroid Build Coastguard Workerecho "BUILD_ENVIRONMENT:$BUILD_ENVIRONMENT"
6*da0073e9SAndroid Build Coastguard Worker
7*da0073e9SAndroid Build Coastguard Workerexport ANDROID_NDK_HOME=/opt/ndk
8*da0073e9SAndroid Build Coastguard Workerexport ANDROID_NDK=/opt/ndk
9*da0073e9SAndroid Build Coastguard Workerexport ANDROID_HOME=/opt/android/sdk
10*da0073e9SAndroid Build Coastguard Worker
11*da0073e9SAndroid Build Coastguard Worker# Must be in sync with GRADLE_VERSION in docker image for android
12*da0073e9SAndroid Build Coastguard Worker# https://github.com/pietern/pytorch-dockerfiles/blob/master/build.sh#L155
13*da0073e9SAndroid Build Coastguard Workerexport GRADLE_VERSION=6.8.3
14*da0073e9SAndroid Build Coastguard Workerexport GRADLE_HOME=/opt/gradle/gradle-$GRADLE_VERSION
15*da0073e9SAndroid Build Coastguard Workerexport GRADLE_PATH=$GRADLE_HOME/bin/gradle
16*da0073e9SAndroid Build Coastguard Worker
17*da0073e9SAndroid Build Coastguard Worker# touch gradle cache files to prevent expiration
18*da0073e9SAndroid Build Coastguard Workerwhile IFS= read -r -d '' file
19*da0073e9SAndroid Build Coastguard Workerdo
20*da0073e9SAndroid Build Coastguard Worker  touch "$file" || true
21*da0073e9SAndroid Build Coastguard Workerdone < <(find /var/lib/jenkins/.gradle -type f -print0)
22*da0073e9SAndroid Build Coastguard Worker
23*da0073e9SAndroid Build Coastguard Worker# Patch pocketfft (as Android does not have aligned_alloc even if compiled with c++17
24*da0073e9SAndroid Build Coastguard Workerif [ -f ~/workspace/third_party/pocketfft/pocketfft_hdronly.h ]; then
25*da0073e9SAndroid Build Coastguard Worker  sed -i -e "s/__cplusplus >= 201703L/0/" ~/workspace/third_party/pocketfft/pocketfft_hdronly.h
26*da0073e9SAndroid Build Coastguard Workerfi
27*da0073e9SAndroid Build Coastguard Worker
28*da0073e9SAndroid Build Coastguard Workerexport GRADLE_LOCAL_PROPERTIES=~/workspace/android/local.properties
29*da0073e9SAndroid Build Coastguard Workerrm -f $GRADLE_LOCAL_PROPERTIES
30*da0073e9SAndroid Build Coastguard Workerecho "sdk.dir=/opt/android/sdk" >> $GRADLE_LOCAL_PROPERTIES
31*da0073e9SAndroid Build Coastguard Workerecho "ndk.dir=/opt/ndk" >> $GRADLE_LOCAL_PROPERTIES
32*da0073e9SAndroid Build Coastguard Workerecho "cmake.dir=/usr/local" >> $GRADLE_LOCAL_PROPERTIES
33*da0073e9SAndroid Build Coastguard Worker
34*da0073e9SAndroid Build Coastguard Workerretry () {
35*da0073e9SAndroid Build Coastguard Worker  $* || (sleep 1 && $*) || (sleep 2 && $*) || (sleep 4 && $*) || (sleep 8 && $*)
36*da0073e9SAndroid Build Coastguard Worker}
37*da0073e9SAndroid Build Coastguard Worker
38*da0073e9SAndroid Build Coastguard Worker# Run custom build script
39*da0073e9SAndroid Build Coastguard Workerif [[ "${BUILD_ENVIRONMENT}" == *-gradle-custom-build* ]]; then
40*da0073e9SAndroid Build Coastguard Worker  # Install torch & torchvision - used to download & dump used ops from test model.
41*da0073e9SAndroid Build Coastguard Worker  retry pip install torch torchvision --progress-bar off
42*da0073e9SAndroid Build Coastguard Worker
43*da0073e9SAndroid Build Coastguard Worker  exec "$(dirname "${BASH_SOURCE[0]}")/../android/build_test_app_custom.sh" armeabi-v7a
44*da0073e9SAndroid Build Coastguard Workerfi
45*da0073e9SAndroid Build Coastguard Worker
46*da0073e9SAndroid Build Coastguard Worker# Run default build
47*da0073e9SAndroid Build Coastguard WorkerBUILD_ANDROID_INCLUDE_DIR_x86=~/workspace/build_android/install/include
48*da0073e9SAndroid Build Coastguard WorkerBUILD_ANDROID_LIB_DIR_x86=~/workspace/build_android/install/lib
49*da0073e9SAndroid Build Coastguard Worker
50*da0073e9SAndroid Build Coastguard WorkerBUILD_ANDROID_INCLUDE_DIR_x86_64=~/workspace/build_android_install_x86_64/install/include
51*da0073e9SAndroid Build Coastguard WorkerBUILD_ANDROID_LIB_DIR_x86_64=~/workspace/build_android_install_x86_64/install/lib
52*da0073e9SAndroid Build Coastguard Worker
53*da0073e9SAndroid Build Coastguard WorkerBUILD_ANDROID_INCLUDE_DIR_arm_v7a=~/workspace/build_android_install_arm_v7a/install/include
54*da0073e9SAndroid Build Coastguard WorkerBUILD_ANDROID_LIB_DIR_arm_v7a=~/workspace/build_android_install_arm_v7a/install/lib
55*da0073e9SAndroid Build Coastguard Worker
56*da0073e9SAndroid Build Coastguard WorkerBUILD_ANDROID_INCLUDE_DIR_arm_v8a=~/workspace/build_android_install_arm_v8a/install/include
57*da0073e9SAndroid Build Coastguard WorkerBUILD_ANDROID_LIB_DIR_arm_v8a=~/workspace/build_android_install_arm_v8a/install/lib
58*da0073e9SAndroid Build Coastguard Worker
59*da0073e9SAndroid Build Coastguard WorkerPYTORCH_ANDROID_SRC_MAIN_DIR=~/workspace/android/pytorch_android/src/main
60*da0073e9SAndroid Build Coastguard Worker
61*da0073e9SAndroid Build Coastguard WorkerJNI_INCLUDE_DIR=${PYTORCH_ANDROID_SRC_MAIN_DIR}/cpp/libtorch_include
62*da0073e9SAndroid Build Coastguard Workermkdir -p $JNI_INCLUDE_DIR
63*da0073e9SAndroid Build Coastguard Worker
64*da0073e9SAndroid Build Coastguard WorkerJNI_LIBS_DIR=${PYTORCH_ANDROID_SRC_MAIN_DIR}/jniLibs
65*da0073e9SAndroid Build Coastguard Workermkdir -p $JNI_LIBS_DIR
66*da0073e9SAndroid Build Coastguard Worker
67*da0073e9SAndroid Build Coastguard Workerln -s ${BUILD_ANDROID_INCLUDE_DIR_x86} ${JNI_INCLUDE_DIR}/x86
68*da0073e9SAndroid Build Coastguard Workerln -s ${BUILD_ANDROID_LIB_DIR_x86} ${JNI_LIBS_DIR}/x86
69*da0073e9SAndroid Build Coastguard Worker
70*da0073e9SAndroid Build Coastguard Workerif [[ "${BUILD_ENVIRONMENT}" != *-gradle-build-only-x86_32* ]]; then
71*da0073e9SAndroid Build Coastguard Workerln -s ${BUILD_ANDROID_INCLUDE_DIR_x86_64} ${JNI_INCLUDE_DIR}/x86_64
72*da0073e9SAndroid Build Coastguard Workerln -s ${BUILD_ANDROID_LIB_DIR_x86_64} ${JNI_LIBS_DIR}/x86_64
73*da0073e9SAndroid Build Coastguard Worker
74*da0073e9SAndroid Build Coastguard Workerln -s ${BUILD_ANDROID_INCLUDE_DIR_arm_v7a} ${JNI_INCLUDE_DIR}/armeabi-v7a
75*da0073e9SAndroid Build Coastguard Workerln -s ${BUILD_ANDROID_LIB_DIR_arm_v7a} ${JNI_LIBS_DIR}/armeabi-v7a
76*da0073e9SAndroid Build Coastguard Worker
77*da0073e9SAndroid Build Coastguard Workerln -s ${BUILD_ANDROID_INCLUDE_DIR_arm_v8a} ${JNI_INCLUDE_DIR}/arm64-v8a
78*da0073e9SAndroid Build Coastguard Workerln -s ${BUILD_ANDROID_LIB_DIR_arm_v8a} ${JNI_LIBS_DIR}/arm64-v8a
79*da0073e9SAndroid Build Coastguard Workerfi
80*da0073e9SAndroid Build Coastguard Worker
81*da0073e9SAndroid Build Coastguard WorkerGRADLE_PARAMS="-p android assembleRelease --debug --stacktrace"
82*da0073e9SAndroid Build Coastguard Workerif [[ "${BUILD_ENVIRONMENT}" == *-gradle-build-only-x86_32* ]]; then
83*da0073e9SAndroid Build Coastguard Worker    GRADLE_PARAMS+=" -PABI_FILTERS=x86"
84*da0073e9SAndroid Build Coastguard Workerfi
85*da0073e9SAndroid Build Coastguard Worker
86*da0073e9SAndroid Build Coastguard Workerif [ -n "${GRADLE_OFFLINE:-}" ]; then
87*da0073e9SAndroid Build Coastguard Worker    GRADLE_PARAMS+=" --offline"
88*da0073e9SAndroid Build Coastguard Workerfi
89*da0073e9SAndroid Build Coastguard Worker
90*da0073e9SAndroid Build Coastguard Worker$GRADLE_PATH $GRADLE_PARAMS
91*da0073e9SAndroid Build Coastguard Worker
92*da0073e9SAndroid Build Coastguard Workerfind . -type f -name "*.a" -exec ls -lh {} \;
93*da0073e9SAndroid Build Coastguard Worker
94*da0073e9SAndroid Build Coastguard Workerwhile IFS= read -r -d '' file
95*da0073e9SAndroid Build Coastguard Workerdo
96*da0073e9SAndroid Build Coastguard Worker  echo
97*da0073e9SAndroid Build Coastguard Worker  echo "$file"
98*da0073e9SAndroid Build Coastguard Worker  ls -lah "$file"
99*da0073e9SAndroid Build Coastguard Worker  zipinfo -l "$file"
100*da0073e9SAndroid Build Coastguard Workerdone < <(find . -type f -name '*.aar' -print0)
101*da0073e9SAndroid Build Coastguard Worker
102*da0073e9SAndroid Build Coastguard Workerfind . -type f -name *aar -print | xargs tar cfvz ~/workspace/android/artifacts.tgz
103