|
14 | 14 | # See the License for the specific language governing permissions and
|
15 | 15 | # limitations under the License.
|
16 | 16 | #
|
17 |
| -# Builds and runs the tests, meant to be used on a bash environment. |
18 |
| - |
19 |
| -# Stop display commands being run. |
20 |
| -set +x |
21 |
| - |
22 |
| -# Set exit code to first failure |
23 |
| -FIRST_FAILED_EXITCODE=0 |
24 |
| - |
25 |
| -function check_exit_code { |
26 |
| - if [ "$1" -ne "0" ] && [ "$FIRST_FAILED_EXITCODE" -eq 0 ]; then |
27 |
| - FIRST_FAILED_EXITCODE=$1 |
28 |
| - fi |
| 17 | +# Builds the Android version of the Firebase Unity SDK. |
| 18 | + |
| 19 | +help() { |
| 20 | + echo "Usage: $(basename "$0") -a arch_list -c cmake_extra -d -h |
| 21 | +Builds the Android SDK, possibly merging multiple architectures. |
| 22 | +-a arch_list |
| 23 | + Space separated list of Android architectures to build for. |
| 24 | + These are passed to CMake with the ANDROID_ABI flag. |
| 25 | + If not provided (and -m not used), defaults to (armeabi-v7a). |
| 26 | +-c cmake_extra |
| 27 | + Additional flags to pass to the CMake configure step. |
| 28 | +-d |
| 29 | + Use the default set of architecture flags, |
| 30 | + armeabi-v7a arm64-v8a x86 x86_64 |
| 31 | + If set, the -a flag is ignored. |
| 32 | +-h |
| 33 | + Display this help message. |
| 34 | +" >&1 |
| 35 | + exit 1 |
29 | 36 | }
|
30 | 37 |
|
31 |
| -CMAKE_OPTIONS= |
| 38 | +main() { |
| 39 | + local -a arch_list=(armeabi-v7a) |
| 40 | + local -a cmake_extra= |
| 41 | + local build_default=false |
| 42 | + while getopts "a:c:dh" option "$@"; do |
| 43 | + case "${option}" in |
| 44 | + a ) arch_list="${OPTARG}";; |
| 45 | + c ) cmake_extra="${OPTARG}";; |
| 46 | + d ) build_default=true;; |
| 47 | + h ) help;; |
| 48 | + * ) help;; |
| 49 | + esac |
| 50 | + done |
| 51 | + |
| 52 | + |
| 53 | + if $build_default; then |
| 54 | + arch_list=(armeabi-v7a arm64-v8a x86 x86_64) |
| 55 | + fi |
32 | 56 |
|
33 |
| -if [ -d "../firebase-cpp-sdk" ]; then |
34 |
| - CMAKE_OPTIONS="-DFIREBASE_CPP_SDK_DIR=`realpath ../firebase-cpp-sdk` " |
35 |
| - cd ../firebase-cpp-sdk |
36 |
| - ./gradlew |
37 |
| - cd ../firebase-unity-sdk |
38 |
| -fi |
| 57 | + # Set exit code to first failure |
| 58 | + FIRST_FAILED_EXITCODE=0 |
39 | 59 |
|
40 |
| -if [[ -z "${ANDROID_NDK_HOME}" ]]; then #ANDROID_NDK_HOME not set |
41 |
| - echo "Using ANDROID_NDK_HOME: ${ANDROID_NDK_HOME} android tool chain" |
42 |
| - shopt -s nullglob |
43 |
| - list=(${ANDROID_HOME}/**/build/cmake/android.toolchain.cmake) |
44 |
| - shopt -u nullglob |
| 60 | + function check_exit_code { |
| 61 | + if [ "$1" -ne "0" ] && [ "$FIRST_FAILED_EXITCODE" -eq 0 ]; then |
| 62 | + FIRST_FAILED_EXITCODE=$1 |
| 63 | + fi |
| 64 | + } |
| 65 | + |
| 66 | + CMAKE_OPTIONS= |
| 67 | + if [ -d "../firebase-cpp-sdk" ]; then |
| 68 | + CMAKE_OPTIONS="-DFIREBASE_CPP_SDK_DIR=`realpath ../firebase-cpp-sdk` " |
| 69 | + cd ../firebase-cpp-sdk |
| 70 | + ./gradlew |
| 71 | + cd ../firebase-unity-sdk |
| 72 | + fi |
45 | 73 |
|
46 |
| - if [ ! -f "${list[0]}" ]; then |
47 |
| - # Some installations of the NDK have it in NDK/version, instead of just |
48 |
| - # ndk-bundle, and ** sometimes does not recurse correctly, so check that case. |
| 74 | + if [[ -z "${ANDROID_NDK_HOME}" ]]; then #ANDROID_NDK_HOME not set |
| 75 | + echo "Using ANDROID_NDK_HOME: ${ANDROID_NDK_HOME} android tool chain" |
49 | 76 | shopt -s nullglob
|
50 |
| - list=(${ANDROID_HOME}/*/*/build/cmake/android.toolchain.cmake) |
| 77 | + list=(${ANDROID_HOME}/**/build/cmake/android.toolchain.cmake) |
51 | 78 | shopt -u nullglob
|
52 | 79 |
|
53 | 80 | if [ ! -f "${list[0]}" ]; then
|
54 |
| - echo "Failed to find android.toolchain.cmake. Please ensure ANDROID_HOME is set to a valid ndk directory." |
55 |
| - exit -1 |
56 |
| - fi |
| 81 | + # Some installations of the NDK have it in NDK/version, instead of just |
| 82 | + # ndk-bundle, and ** sometimes does not recurse correctly, so check that case. |
| 83 | + shopt -s nullglob |
| 84 | + list=(${ANDROID_HOME}/*/*/build/cmake/android.toolchain.cmake) |
| 85 | + shopt -u nullglob |
| 86 | + |
| 87 | + if [ ! -f "${list[0]}" ]; then |
| 88 | + echo "Failed to find android.toolchain.cmake. Please ensure ANDROID_HOME is set to a valid ndk directory." |
| 89 | + exit -1 |
| 90 | + fi |
| 91 | + fi |
| 92 | + else |
| 93 | + echo "Using ANDROID_NDK_HOME: ${ANDROID_NDK_HOME} android tool chain" |
| 94 | + shopt -s nullglob |
| 95 | + list=(${ANDROID_NDK_HOME}/build/cmake/android.toolchain.cmake) |
| 96 | + shopt -u nullglob |
57 | 97 | fi
|
58 |
| -else |
59 |
| - echo "Using ANDROID_NDK_HOME: ${ANDROID_NDK_HOME} android tool chain" |
60 |
| - shopt -s nullglob |
61 |
| - list=(${ANDROID_NDK_HOME}/build/cmake/android.toolchain.cmake) |
62 |
| - shopt -u nullglob |
63 |
| -fi |
| 98 | + echo "Using android toolchain: ${list[0]}" |
64 | 99 |
|
65 |
| -echo "Using android toolchain: ${list[0]}" |
| 100 | + if [ -n "$UNITY_ROOT_DIR" ]; then |
| 101 | + CMAKE_OPTIONS="${CMAKE_OPTIONS} -DUNITY_ROOT_DIR=${UNITY_ROOT_DIR}" |
| 102 | + fi |
| 103 | + CMAKE_OPTIONS="${CMAKE_OPTIONS} -DANDROID_NDK=$ANDROID_NDK_HOME" |
| 104 | + CMAKE_OPTIONS="${CMAKE_OPTIONS} -DCMAKE_TOOLCHAIN_FILE=${list[0]}" |
| 105 | + CMAKE_OPTIONS="${CMAKE_OPTIONS} -DFIREBASE_ANDROID_BUILD=true" |
| 106 | + CMAKE_OPTIONS="${CMAKE_OPTIONS} -DCMAKE_BUILD_TYPE=release" |
66 | 107 |
|
67 |
| -CMAKE_OPTIONS="${CMAKE_OPTIONS} -DUNITY_ROOT_DIR=${UNITY_ROOT_DIR}" |
68 |
| -CMAKE_OPTIONS="${CMAKE_OPTIONS} -DANDROID_NDK=$ANDROID_NDK" |
69 |
| -CMAKE_OPTIONS="${CMAKE_OPTIONS} -DCMAKE_TOOLCHAIN_FILE=${list[0]}" |
70 |
| -CMAKE_OPTIONS="${CMAKE_OPTIONS} -DANDROID_ABI=armeabi-v7a" |
71 |
| -CMAKE_OPTIONS="${CMAKE_OPTIONS} -DFIREBASE_ANDROID_BUILD=true" |
72 |
| -CMAKE_OPTIONS="${CMAKE_OPTIONS} -DCMAKE_BUILD_TYPE=release" |
| 108 | + # Make a directory to work in (if doesn't exist) |
| 109 | + mkdir -p android_build |
73 | 110 |
|
74 |
| -# Display commands being run. |
75 |
| -set -x |
| 111 | + pushd android_build |
76 | 112 |
|
77 |
| -# Make a directory to work in (if doesn't exist) |
78 |
| -mkdir -p android_build |
| 113 | + multi_arch=false |
| 114 | + path_up=".." |
| 115 | + if [ "${#arch_list[@]}" -gt 1 ]; then |
| 116 | + multi_arch=true |
| 117 | + path_up="../.." |
| 118 | + fi |
| 119 | + for arch in "${arch_list[@]}"; do |
| 120 | + # If building more than one architecture, move into a subdirectory |
| 121 | + if $multi_arch; then |
| 122 | + mkdir -p "${arch}" |
| 123 | + pushd "${arch}" |
| 124 | + fi |
79 | 125 |
|
80 |
| -pushd android_build |
| 126 | + # Configure cmake with option value |
| 127 | + cmake $path_up ${CMAKE_OPTIONS} -DANDROID_ABI="${arch}" ${cmake_extra} |
| 128 | + check_exit_code $? |
81 | 129 |
|
82 |
| -# Configure cmake with option value |
83 |
| -cmake .. ${CMAKE_OPTIONS} "$@" |
84 |
| -check_exit_code $? |
| 130 | + # Build the SDK |
| 131 | + make |
| 132 | + check_exit_code $? |
85 | 133 |
|
86 |
| -# Build the SDK |
87 |
| -make # -j 8 |
88 |
| -check_exit_code $? |
| 134 | + # Package build output into zip |
| 135 | + cpack . |
| 136 | + check_exit_code $? |
89 | 137 |
|
90 |
| -# Package build output into zip |
91 |
| -cpack . |
92 |
| -check_exit_code $? |
| 138 | + if $multi_arch; then |
| 139 | + popd |
| 140 | + fi |
| 141 | + done |
| 142 | + |
| 143 | + # If building for multiple architectures, merge the results |
| 144 | + if $multi_arch; then |
| 145 | + local -r unzip_dir="$(mktemp -d)" |
| 146 | + local -r remove_temp_dir="rm -rf \"${unzip_dir}\"" |
| 147 | + trap "${remove_temp_dir}" SIGKILL SIGTERM SIGQUIT EXIT |
| 148 | + |
| 149 | + zip_basename= |
| 150 | + srcaar_list= |
| 151 | + for arch in "${arch_list[@]}"; do |
| 152 | + zip_name=$(find ${arch} -name '*Android.zip' -depth 1) |
| 153 | + |
| 154 | + if [ -z "${zip_basename}" ]; then |
| 155 | + # If this is the first srcaar being handled, unzip everything |
| 156 | + # to the primary unzip_dir. |
| 157 | + unzip -q -d "${unzip_dir}" "${zip_name}" |
| 158 | + |
| 159 | + # Get the list of srcaar files, to merge into later |
| 160 | + srcaar_list=$(find ${unzip_dir} -type f -name '*.srcaar') |
| 161 | + zip_basename=$(basename ${zip_name}) |
| 162 | + else |
| 163 | + # Unzip the other architectures into a different temp directory |
| 164 | + tmp_dir="$(mktemp -d)" |
| 165 | + echo "TMPDIR: ${tmp_dir}" |
| 166 | + trap "rm -rf ${tmp_dir}" SIGKILL SIGTERM SIGQUIT EXIT |
| 167 | + unzip -q -d "${tmp_dir}" "${zip_name}" *.srcaar |
| 168 | + for srcaar in "${srcaar_list[@]}"; do |
| 169 | + # Get the path to the new srcaar that matches the original |
| 170 | + srcaar_name=$(basename ${srcaar}) |
| 171 | + echo "Looking for ${srcaar_name}" |
| 172 | + new_srcaar=$(find ${tmp_dir} | grep ${srcaar_name}) |
| 173 | + # Merge into the first srcaar |
| 174 | + ../aar_builder/merge_aar.sh -i "${srcaar} ${new_srcaar}" -o ${srcaar} |
| 175 | + done |
| 176 | + fi |
| 177 | + done |
| 178 | + |
| 179 | + # Zip up the result into a new zipfile with the correct name |
| 180 | + final_zip="$(pwd)/${zip_basename}" |
| 181 | + pushd ${unzip_dir} |
| 182 | + zip -q -X -r ${final_zip} . |
| 183 | + popd |
| 184 | + fi |
93 | 185 |
|
94 |
| -# Stop display commands being run. |
95 |
| -set +x |
| 186 | + # popd android_build |
| 187 | + popd |
96 | 188 |
|
97 |
| -popd |
| 189 | + exit $FIRST_FAILED_EXITCODE |
| 190 | +} |
98 | 191 |
|
99 |
| -exit $FIRST_FAILED_EXITCODE |
| 192 | +main "$@" |
0 commit comments