xref: /aosp_15_r20/external/executorch/examples/apple/coreml/scripts/build_executor_runner.sh (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1#!/usr/bin/env bash
2#
3# Copyright © 2023 Apple Inc. All rights reserved.
4#
5# This source code is licensed under the BSD-style license found in the
6# LICENSE file in the root directory of this source tree.
7
8SCRIPT_DIR_PATH="$(
9    cd -- "$(dirname "$0")" >/dev/null 2>&1
10    pwd -P
11)"
12
13EXECUTORCH_ROOT_PATH=$(realpath "$SCRIPT_DIR_PATH/../../../../")
14COREML_DIR_PATH="$EXECUTORCH_ROOT_PATH/backends/apple/coreml"
15EXAMPLES_COREML_DIR_PATH="$EXECUTORCH_ROOT_PATH/examples/apple/coreml"
16IOS_TOOLCHAIN_PATH="$EXECUTORCH_ROOT_PATH/third-party/ios-cmake/ios.toolchain.cmake"
17CMAKE_BUILD_DIR_PATH="$EXAMPLES_COREML_DIR_PATH/cmake-out"
18LIBRARIES_DIR_PATH="$EXAMPLES_COREML_DIR_PATH/executor_runner/libraries"
19INCLUDE_DIR_PATH="$EXAMPLES_COREML_DIR_PATH/executor_runner/include"
20EXECUTORCH_INCLUDE_DIR_PATH="$COREML_DIR_PATH/runtime/include/executorch"
21
22cd "$EXECUTORCH_ROOT_PATH"
23
24echo "ExecuTorch: Building executor_runner"
25
26echo "ExecuTorch: Removing build directory $CMAKE_BUILD_DIR"
27rm -rf "$CMAKE_BUILD_DIR_PATH"
28
29# Build executorch
30echo "ExecuTorch: Building executorch"
31cmake "$EXECUTORCH_ROOT_PATH" -B"$CMAKE_BUILD_DIR_PATH" \
32-DCMAKE_BUILD_TYPE=Release \
33-DCMAKE_TOOLCHAIN_FILE="$IOS_TOOLCHAIN_PATH" \
34-DPLATFORM=MAC_UNIVERSAL \
35-DDEPLOYMENT_TARGET=13.0 \
36-DFLATC_EXECUTABLE="$(which flatc)" \
37-DEXECUTORCH_BUILD_EXECUTOR_RUNNER=OFF \
38-DEXECUTORCH_BUILD_XNNPACK=OFF \
39-DEXECUTORCH_BUILD_DEVTOOLS=ON \
40-DEXECUTORCH_BUILD_COREML=ON \
41-Dprotobuf_BUILD_TESTS=OFF \
42-Dprotobuf_BUILD_EXAMPLES=OFF \
43-DCOREML_BUILD_EXECUTOR_RUNNER=ON \
44-DCMAKE_MACOSX_BUNDLE=OFF \
45
46cmake --build "$CMAKE_BUILD_DIR_PATH" -j9 -t coremldelegate
47cmake --build "$CMAKE_BUILD_DIR_PATH" -j9 -t etdump -t flatccrt
48
49# Copy CoreML delegate headers
50echo "ExecuTorch: Copying headers"
51echo $EXECUTORCH_INCLUDE_DIR_PATH
52rm -rf "$INCLUDE_DIR_PATH"
53mkdir "$INCLUDE_DIR_PATH"
54#Copy ExecuTorch headers
55mkdir -p "$EXECUTORCH_INCLUDE_DIR_PATH"
56find extension \( -name "*.h" -o -name "*.hpp" \) -exec rsync -R '{}' "$EXECUTORCH_INCLUDE_DIR_PATH" \;
57find runtime \( -name "*.h" -o -name "*.hpp" \) -exec rsync -R '{}' "$EXECUTORCH_INCLUDE_DIR_PATH" \;
58find util \( -name "*.h" -o -name "*.hpp" \) -exec rsync -R '{}' "$EXECUTORCH_INCLUDE_DIR_PATH" \;
59find devtools \( -name "*.h" -o -name "*.hpp" \) -exec rsync -R '{}' "$EXECUTORCH_INCLUDE_DIR_PATH" \;
60cp -rf "$COREML_DIR_PATH/runtime/include/" "$INCLUDE_DIR_PATH"
61
62# Copy required libraries
63echo "ExecuTorch: Copying libraries"
64mkdir "$LIBRARIES_DIR_PATH"
65find "$CMAKE_BUILD_DIR_PATH/" -name 'libexecutorch.a' -exec cp -f "{}" "$LIBRARIES_DIR_PATH/libexecutorch.a"  \;
66find "$CMAKE_BUILD_DIR_PATH/" -name 'libexecutorch_core.a' -exec cp -f "{}" "$LIBRARIES_DIR_PATH/libexecutorch_core.a"  \;
67find "$CMAKE_BUILD_DIR_PATH/" -name 'libprotobuf-lite.a' -exec cp -f "{}" "$LIBRARIES_DIR_PATH/libprotobuf-lite.a"  \;
68find "$CMAKE_BUILD_DIR_PATH/" -name 'libprotobuf-lited.a' -exec cp -f "{}" "$LIBRARIES_DIR_PATH/libprotobuf-lite.a"  \;
69find "$CMAKE_BUILD_DIR_PATH/" -name 'libetdump.a' -exec cp -f "{}" "$LIBRARIES_DIR_PATH/libetdump.a"  \;
70find "$CMAKE_BUILD_DIR_PATH/" -name 'libcoremldelegate.a' -exec cp -f "{}" "$LIBRARIES_DIR_PATH/libcoremldelegate.a"  \;
71find "$CMAKE_BUILD_DIR_PATH/" -name 'libportable_ops_lib.a' -exec cp -f "{}" "$LIBRARIES_DIR_PATH/libportable_ops_lib.a"  \;
72find "$CMAKE_BUILD_DIR_PATH/" -name 'libportable_kernels.a' -exec cp -f "{}" "$LIBRARIES_DIR_PATH/libportable_kernels.a"  \;
73cp -f "$EXECUTORCH_ROOT_PATH/third-party/flatcc/lib/libflatccrt.a" "$LIBRARIES_DIR_PATH/libflatccrt.a"
74
75# Build the runner
76echo "ExecuTorch: Building runner"
77XCODE_WORKSPACE_DIR_PATH="$EXAMPLES_COREML_DIR_PATH/executor_runner"
78XCODE_BUILD_DIR_PATH="$EXAMPLES_COREML_DIR_PATH/xcode-build"
79
80xcodebuild build -workspace "$XCODE_WORKSPACE_DIR_PATH/coreml_executor_runner.xcworkspace" -scheme coreml_executor_runner BUILD_DIR="$XCODE_BUILD_DIR_PATH"
81cp -f "$XCODE_BUILD_DIR_PATH/DEBUG/coreml_executor_runner" "$PWD"
82