forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_apple_frameworks.sh
More file actions
executable file
·293 lines (252 loc) · 8.65 KB
/
Copy pathbuild_apple_frameworks.sh
File metadata and controls
executable file
·293 lines (252 loc) · 8.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#!/bin/bash
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
set -euxo pipefail
SOURCE_ROOT_DIR=""
OUTPUT="cmake-out"
MODES=()
TOOLCHAIN=""
PYTHON=$(which python3)
COREML=OFF
CUSTOM=OFF
MPS=OFF
OPTIMIZED=OFF
PORTABLE=OFF
QUANTIZED=OFF
XNNPACK=OFF
HEADERS_PATH="include"
PLATFORMS=("ios" "simulator" "macos")
PLATFORM_FLAGS=("OS64" "SIMULATORARM64" "MAC_ARM64")
PLATFORM_TARGET=("17.0" "17.0" "10.15")
FRAMEWORK_EXECUTORCH="executorch:\
libexecutorch.a,\
libexecutorch_core.a,\
libextension_apple.a,\
libextension_data_loader.a,\
libextension_module.a,\
libextension_tensor.a,\
:$HEADERS_PATH"
FRAMEWORK_BACKEND_COREML="backend_coreml:\
libcoreml_util.a,\
libcoreml_inmemoryfs.a,\
libcoremldelegate.a,\
:"
FRAMEWORK_BACKEND_MPS="backend_mps:\
libmpsdelegate.a,\
:"
FRAMEWORK_BACKEND_XNNPACK="backend_xnnpack:\
libXNNPACK.a,\
libcpuinfo.a,\
libextension_threadpool.a,\
libpthreadpool.a,\
libxnnpack_backend.a,\
libmicrokernels-prod.a,\
:"
FRAMEWORK_KERNELS_CUSTOM="kernels_custom:\
libcustom_ops.a,\
:"
FRAMEWORK_KERNELS_OPTIMIZED="kernels_optimized:\
libcpublas.a,\
liboptimized_kernels.a,\
liboptimized_native_cpu_ops_lib.a,\
:"
FRAMEWORK_KERNELS_PORTABLE="kernels_portable:\
libportable_kernels.a,\
libportable_ops_lib.a,\
:"
FRAMEWORK_KERNELS_QUANTIZED="kernels_quantized:\
libquantized_kernels.a,\
libquantized_ops_lib.a,\
:"
usage() {
echo "Usage: $0 [SOURCE_ROOT_DIR] [OPTIONS]"
echo "Build frameworks for Apple platforms."
echo "SOURCE_ROOT_DIR defaults to the current directory if not provided."
echo
echo "Options:"
echo " --output=DIR Output directory. Default: 'cmake-out'"
echo " --Debug Build Debug version."
echo " --Release Build Release version."
echo " --toolchain=FILE CMake toolchain file. Default: '\$SOURCE_ROOT_DIR/third-party/ios-cmake/ios.toolchain.cmake'"
echo " --python=FILE Python executable path. Default: Path of python3 in \$PATH"
echo " --coreml Build the Core ML backend."
echo " --custom Build the Custom kernels."
echo " --mps Build the Metal Performance Shaders backend."
echo " --optimized Build the Optimized kernels."
echo " --portable Build the Portable kernels."
echo " --quantized Build the Quantized kernels."
echo " --xnnpack Build the XNNPACK backend."
echo
echo "Example:"
echo " $0 /path/to/source/root --output=cmake-out --toolchain=/path/to/toolchain --python=/path/to/python3 --coreml --mps --xnnpack"
exit 0
}
for arg in "$@"; do
case $arg in
-h|--help) usage ;;
--output=*) OUTPUT="${arg#*=}" ;;
--Release)
if [[ ! " ${MODES[*]:-} " =~ \bRelease\b ]]; then
MODES+=("Release")
fi
;;
--Debug)
if [[ ! " ${MODES[*]:-} " =~ \bDebug\b ]]; then
MODES+=("Debug")
fi
;;
--toolchain=*) TOOLCHAIN="${arg#*=}" ;;
--python=*) PYTHON="${arg#*=}" ;;
--coreml) COREML=ON ;;
--custom) CUSTOM=ON ;;
--mps) MPS=ON ;;
--optimized) OPTIMIZED=ON ;;
--portable) PORTABLE=ON ;;
--quantized) QUANTIZED=ON ;;
--xnnpack) XNNPACK=ON ;;
*)
if [[ -z "$SOURCE_ROOT_DIR" ]]; then
SOURCE_ROOT_DIR="$arg"
else
echo "Invalid argument: $arg"
exit 1
fi
;;
esac
done
if [ ${#MODES[@]} -eq 0 ]; then
MODES=("Release")
fi
if [[ -z "$SOURCE_ROOT_DIR" ]]; then
SOURCE_ROOT_DIR=$(pwd)
fi
if [[ -z "$TOOLCHAIN" ]]; then
TOOLCHAIN="$SOURCE_ROOT_DIR/third-party/ios-cmake/ios.toolchain.cmake"
fi
[[ -f "$TOOLCHAIN" ]] || { echo >&2 "Toolchain file $TOOLCHAIN does not exist."; exit 1; }
BUCK2=$("$PYTHON" "$SOURCE_ROOT_DIR/tools/cmake/resolve_buck.py" --cache_dir="$SOURCE_ROOT_DIR/buck2-bin")
if [[ "$BUCK2" == "buck2" ]]; then
BUCK2=$(command -v buck2)
fi
check_command() {
if [[ "$1" == */* ]]; then
if [[ ! -x "$1" ]]; then
echo "Error: Command not found or not executable at '$1'" >&2
exit 1
fi
else
if ! command -v "$1" >/dev/null 2>&1; then
echo "Error: Command '$1' not found in PATH" >&2
exit 1
fi
fi
}
check_command cmake
check_command rsync
check_command "$PYTHON"
check_command "$BUCK2"
echo "Building libraries"
rm -rf "$OUTPUT" && mkdir -p "$OUTPUT" && cd "$OUTPUT" || exit 1
cmake_build() {
local platform=$1
local platform_flag=$2
local platform_target=$3
local mode=$4
echo "Building for $platform ($mode) with flag $platform_flag"
mkdir -p "$platform" && cd "$platform" || exit 1
cmake "$SOURCE_ROOT_DIR" -G Xcode \
-DCMAKE_BUILD_TYPE="$mode" \
-DCMAKE_TOOLCHAIN_FILE="$TOOLCHAIN" \
-DCMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LANGUAGE_STANDARD="c++17" \
-DCMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY="libc++" \
-DCMAKE_C_FLAGS="-ffile-prefix-map=$SOURCE_ROOT_DIR=/executorch -fdebug-prefix-map=$SOURCE_ROOT_DIR=/executorch" \
-DCMAKE_CXX_FLAGS="-ffile-prefix-map=$SOURCE_ROOT_DIR=/executorch -fdebug-prefix-map=$SOURCE_ROOT_DIR=/executorch" \
-DPYTHON_EXECUTABLE="$PYTHON" \
-DEXECUTORCH_BUILD_COREML=$COREML \
-DEXECUTORCH_BUILD_MPS=$MPS \
-DEXECUTORCH_BUILD_XNNPACK=$XNNPACK \
-DEXECUTORCH_XNNPACK_SHARED_WORKSPACE=ON \
-DEXECUTORCH_BUILD_EXTENSION_APPLE=ON \
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
-DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON \
-DEXECUTORCH_BUILD_KERNELS_CUSTOM=$CUSTOM \
-DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=$OPTIMIZED \
-DEXECUTORCH_BUILD_KERNELS_QUANTIZED=$QUANTIZED \
-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY="$(pwd)" \
${platform_flag:+-DPLATFORM=$platform_flag} \
${platform_target:+-DDEPLOYMENT_TARGET=$platform_target} \
--log-level=VERBOSE
cmake --build . \
--config "$mode" \
--verbose
cd -
}
for index in ${!PLATFORMS[*]}; do
for mode in "${MODES[@]}"; do
cmake_build "${PLATFORMS[$index]}" "${PLATFORM_FLAGS[$index]}" "${PLATFORM_TARGET[$index]}" "$mode"
done
done
echo "Exporting headers"
mkdir -p "$HEADERS_PATH"
"$SOURCE_ROOT_DIR"/scripts/print_exported_headers.py --buck2=$(realpath "$BUCK2") --targets \
//extension/module: \
//extension/tensor: \
| rsync -av --files-from=- "$SOURCE_ROOT_DIR" "$HEADERS_PATH/executorch"
# HACK: XCFrameworks don't appear to support exporting any build
# options, but we need the following:
# - runtime/core/portable/type/c10 reachable with `#include <c10/...>`
# - exported -DC10_USING_CUSTOM_GENERATED_MACROS compiler flag
# So, just patch our generated framework to do that.
sed -i '' '1i\
#define C10_USING_CUSTOM_GENERATED_MACROS
' \
"$HEADERS_PATH/executorch/runtime/core/portable_type/c10/c10/macros/Macros.h" \
"$HEADERS_PATH/executorch/runtime/core/portable_type/c10/c10/macros/Export.h"
cp -r $HEADERS_PATH/executorch/runtime/core/portable_type/c10/c10 "$HEADERS_PATH/"
cp "$SOURCE_ROOT_DIR/extension/apple/ExecuTorch/Exported/"*.h "$HEADERS_PATH/executorch"
cp "$SOURCE_ROOT_DIR/extension/apple/ExecuTorch/Exported/"*.modulemap "$HEADERS_PATH"
echo "Creating frameworks"
append_framework_flag() {
local flag="$1"
local framework="$2"
local mode="${3:-}"
if [[ $flag == ON ]]; then
if [[ -n "$mode" && "$mode" != "Release" ]]; then
local name spec
name=$(echo "$framework" | cut -d: -f1)
spec=$(echo "$framework" | cut -d: -f2-)
framework="${name}_$(echo "$mode" | tr '[:upper:]' '[:lower:]'):${spec}"
fi
echo "Framework: $framework"
FRAMEWORK_FLAGS+=("--framework=$framework")
fi
}
for mode in "${MODES[@]}"; do
FRAMEWORK_FLAGS=()
for platform in "${PLATFORMS[@]}"; do
echo "Directory: $platform/$mode"
FRAMEWORK_FLAGS+=("--directory=$platform/$mode")
done
append_framework_flag "ON" "$FRAMEWORK_EXECUTORCH" "$mode"
append_framework_flag "$COREML" "$FRAMEWORK_BACKEND_COREML" "$mode"
append_framework_flag "$MPS" "$FRAMEWORK_BACKEND_MPS" "$mode"
append_framework_flag "$XNNPACK" "$FRAMEWORK_BACKEND_XNNPACK" "$mode"
append_framework_flag "$CUSTOM" "$FRAMEWORK_KERNELS_CUSTOM" "$mode"
append_framework_flag "$OPTIMIZED" "$FRAMEWORK_KERNELS_OPTIMIZED" "$mode"
append_framework_flag "$PORTABLE" "$FRAMEWORK_KERNELS_PORTABLE" "$mode"
append_framework_flag "$QUANTIZED" "$FRAMEWORK_KERNELS_QUANTIZED" "$mode"
"$SOURCE_ROOT_DIR"/scripts/create_frameworks.sh "${FRAMEWORK_FLAGS[@]}"
done
echo "Cleaning up"
for platform in "${PLATFORMS[@]}"; do
rm -rf "$platform"
done
rm -rf "$HEADERS_PATH"
echo "Running tests"
cd "$SOURCE_ROOT_DIR"
swift test
echo "Build succeeded!"