xref: /aosp_15_r20/external/executorch/examples/mediatek/mtk_build_examples.sh (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1#!/bin/bash
2
3# Exit immediately if a command exits with a non-zero status.
4set -e
5
6# Define the directory where CMakeLists.txt is located
7EXECUTORCH_ROOT=$(realpath "$(dirname "$0")/../..")
8echo EXECUTORCH_ROOT=${EXECUTORCH_ROOT}
9
10# Check if buck2 exists
11BUCK_PATH=${BUCK2:-buck2}
12if [ -z "$BUCK2" ]; then
13    echo "Info: BUCK2 environment variable is not set." >&2
14fi
15
16# Check if the ANDROID_NDK environment variable is set
17if [ -z "$ANDROID_NDK" ]; then
18    echo "Error: ANDROID_NDK environment variable is not set." >&2
19    exit 1
20fi
21
22# Check if the NEURON_BUFFER_ALLOCATOR_LIB environment variable is set
23if [ -z "$NEURON_BUFFER_ALLOCATOR_LIB" ]; then
24    echo "Error: NEURON_BUFFER_ALLOCATOR_LIB environment variable is not set." >&2
25    exit 1
26fi
27
28main() {
29    # Set build directory
30    local build_dir="cmake-android-out"
31
32    # Create and enter the build directory
33    cd "$EXECUTORCH_ROOT"
34    rm -rf "${build_dir}"
35
36    # Configure the project with CMake
37    # Note: Add any additional configuration options you need here
38    cmake -DCMAKE_INSTALL_PREFIX="${build_dir}" \
39          -DBUCK2="$BUCK_PATH" \
40          -DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK/build/cmake/android.toolchain.cmake" \
41          -DANDROID_ABI=arm64-v8a \
42          -DANDROID_NATIVE_API_LEVEL=23 \
43          -DEXECUTORCH_BUILD_NEURON=ON \
44          -DNEURON_BUFFER_ALLOCATOR_LIB="$NEURON_BUFFER_ALLOCATOR_LIB" \
45          -B"${build_dir}"
46
47
48    # Build the project
49    cmake --build cmake-android-out --target install --config Release -j5
50
51    ## Build example
52    local example_dir=examples/mediatek
53    local example_build_dir="${build_dir}/${example_dir}"
54    local cmake_prefix_path="${PWD}/${build_dir}/lib/cmake/ExecuTorch;${PWD}/${build_dir}/third-party/gflags;"
55    rm -rf "${example_build_dir}"
56
57    ## MTK original
58    cmake -DCMAKE_PREFIX_PATH="${cmake_prefix_path}" \
59          -DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK/build/cmake/android.toolchain.cmake" \
60          -DANDROID_ABI=arm64-v8a \
61          -DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=BOTH \
62          -DNEURON_BUFFER_ALLOCATOR_LIB="$NEURON_BUFFER_ALLOCATOR_LIB" \
63          -B"${example_build_dir}" \
64          $EXECUTORCH_ROOT/$example_dir
65
66    cmake --build "${example_build_dir}" -j5
67
68    # Switch back to the original directory
69    cd - > /dev/null
70
71    # Print a success message
72    echo "Build successfully completed."
73}
74
75main "$@"
76