Skip to content

Commit 3261d05

Browse files
authored
Allow building Apple frameworks for multiple modes with one script in… (#8791)
* Allow building Apple frameworks for multiple modes with one script invocation * Update build_apple_frameworks.sh
1 parent 954246c commit 3261d05

File tree

1 file changed

+65
-39
lines changed

1 file changed

+65
-39
lines changed

build/build_apple_frameworks.sh

Lines changed: 65 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ set -euo pipefail
99

1010
SOURCE_ROOT_DIR=""
1111
OUTPUT="cmake-out"
12-
MODE="Release"
12+
MODES=()
1313
TOOLCHAIN=""
1414
PYTHON=$(which python3)
1515
FLATC=$(which flatc)
@@ -77,28 +77,38 @@ usage() {
7777
echo
7878
echo "Options:"
7979
echo " --output=DIR Output directory. Default: 'cmake-out'"
80-
echo " --Debug Use Debug build mode. Default: Uses Release build mode."
81-
echo " --toolchain=FILE Cmake toolchain file. Default: '\$SOURCE_ROOT_DIR/third-party/ios-cmake/ios.toolchain.cmake'"
82-
echo " --python=FILE Python executable path. Default: Path of python3 found in the current \$PATH"
83-
echo " --flatc=FILE FlatBuffers Compiler executable path. Default: Path of flatc found in the current \$PATH"
84-
echo " --coreml Include this flag to build the Core ML backend."
85-
echo " --custom Include this flag to build the Custom kernels."
86-
echo " --mps Include this flag to build the Metal Performance Shaders backend."
87-
echo " --optimized Include this flag to build the Optimized kernels."
88-
echo " --portable Include this flag to build the Portable kernels."
89-
echo " --quantized Include this flag to build the Quantized kernels."
90-
echo " --xnnpack Include this flag to build the XNNPACK backend."
80+
echo " --Debug Build Debug version."
81+
echo " --Release Build Release version."
82+
echo " --toolchain=FILE CMake toolchain file. Default: '\$SOURCE_ROOT_DIR/third-party/ios-cmake/ios.toolchain.cmake'"
83+
echo " --python=FILE Python executable path. Default: Path of python3 in \$PATH"
84+
echo " --flatc=FILE FlatBuffers Compiler executable path. Default: Path of flatc in \$PATH"
85+
echo " --coreml Build the Core ML backend."
86+
echo " --custom Build the Custom kernels."
87+
echo " --mps Build the Metal Performance Shaders backend."
88+
echo " --optimized Build the Optimized kernels."
89+
echo " --portable Build the Portable kernels."
90+
echo " --quantized Build the Quantized kernels."
91+
echo " --xnnpack Build the XNNPACK backend."
9192
echo
9293
echo "Example:"
93-
echo " $0 /path/to/source/root --output=cmake-out --toolchain=/path/to/cmake/toolchain --python=/path/to/python3 --coreml --mps --xnnpack"
94+
echo " $0 /path/to/source/root --output=cmake-out --toolchain=/path/to/toolchain --python=/path/to/python3 --coreml --mps --xnnpack"
9495
exit 0
9596
}
9697

9798
for arg in "$@"; do
9899
case $arg in
99100
-h|--help) usage ;;
100101
--output=*) OUTPUT="${arg#*=}" ;;
101-
--Debug) MODE="Debug" ;;
102+
--Release)
103+
if [[ ! " ${MODES[*]:-} " =~ \bRelease\b ]]; then
104+
MODES+=("Release")
105+
fi
106+
;;
107+
--Debug)
108+
if [[ ! " ${MODES[*]:-} " =~ \bDebug\b ]]; then
109+
MODES+=("Debug")
110+
fi
111+
;;
102112
--toolchain=*) TOOLCHAIN="${arg#*=}" ;;
103113
--python=*) PYTHON="${arg#*=}" ;;
104114
--flatc=*) FLATC="${arg#*=}" ;;
@@ -120,6 +130,10 @@ for arg in "$@"; do
120130
esac
121131
done
122132

133+
if [ ${#MODES[@]} -eq 0 ]; then
134+
MODES=("Release")
135+
fi
136+
123137
if [[ -z "$SOURCE_ROOT_DIR" ]]; then
124138
SOURCE_ROOT_DIR=$(pwd)
125139
fi
@@ -146,10 +160,11 @@ cmake_build() {
146160
local platform=$1
147161
local platform_flag=$2
148162
local platform_target=$3
149-
echo "Building for $platform with flag $platform_flag"
150-
mkdir "$platform" && cd "$platform" || exit 1
163+
local mode=$4
164+
echo "Building for $platform ($mode) with flag $platform_flag"
165+
mkdir -p "$platform" && cd "$platform" || exit 1
151166
cmake "$SOURCE_ROOT_DIR" -G Xcode \
152-
-DCMAKE_BUILD_TYPE="$MODE" \
167+
-DCMAKE_BUILD_TYPE="$mode" \
153168
-DCMAKE_TOOLCHAIN_FILE="$TOOLCHAIN" \
154169
-DCMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LANGUAGE_STANDARD="c++17" \
155170
-DCMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY="libc++" \
@@ -173,13 +188,15 @@ cmake_build() {
173188
${platform_target:+-DDEPLOYMENT_TARGET=$platform_target} \
174189
--log-level=VERBOSE
175190
cmake --build . \
176-
--config $MODE \
191+
--config "$mode" \
177192
--verbose
178193
cd ..
179194
}
180195

181196
for index in ${!PLATFORMS[*]}; do
182-
cmake_build "${PLATFORMS[$index]}" "${PLATFORM_FLAGS[$index]}" "${PLATFORM_TARGET[$index]}"
197+
for mode in "${MODES[@]}"; do
198+
cmake_build "${PLATFORMS[$index]}" "${PLATFORM_FLAGS[$index]}" "${PLATFORM_TARGET[$index]}" "$mode"
199+
done
183200
done
184201

185202
echo "Exporting headers"
@@ -206,42 +223,51 @@ check_command "$BUCK2"
206223
# So, just patch our generated framework to do that.
207224
sed -i '' '1i\
208225
#define C10_USING_CUSTOM_GENERATED_MACROS
209-
' $HEADERS_PATH/executorch/runtime/core/portable_type/c10/c10/macros/Macros.h
210-
sed -i '' '1i\
211-
#define C10_USING_CUSTOM_GENERATED_MACROS
212-
' $HEADERS_PATH/executorch/runtime/core/portable_type/c10/c10/macros/Export.h
213-
cp -r $HEADERS_PATH/executorch/runtime/core/portable_type/c10/c10 "$HEADERS_PATH/"
226+
' \
227+
"$HEADERS_PATH/executorch/runtime/core/portable_type/c10/c10/macros/Macros.h" \
228+
"$HEADERS_PATH/executorch/runtime/core/portable_type/c10/c10/macros/Export.h"
214229

230+
cp -r $HEADERS_PATH/executorch/runtime/core/portable_type/c10/c10 "$HEADERS_PATH/"
215231

216232
cp "$SOURCE_ROOT_DIR/extension/apple/ExecuTorch/Exported/"*.h "$HEADERS_PATH/executorch"
217233
cp "$SOURCE_ROOT_DIR/extension/apple/ExecuTorch/Exported/"*.modulemap "$HEADERS_PATH"
218234

219235
echo "Creating frameworks"
220236

221-
for platform in "${PLATFORMS[@]}"; do
222-
echo "Directory: $platform/$MODE"
223-
FRAMEWORK_FLAGS+=("--directory=$platform/$MODE")
224-
done
225-
226237
append_framework_flag() {
227238
local flag="$1"
228239
local framework="$2"
240+
local mode="${3:-}"
229241
if [[ $flag == ON ]]; then
242+
if [[ -n "$mode" && "$mode" != "Release" ]]; then
243+
local name spec
244+
name=$(echo "$framework" | cut -d: -f1)
245+
spec=$(echo "$framework" | cut -d: -f2-)
246+
framework="${name}_$(echo "$mode" | tr '[:upper:]' '[:lower:]'):${spec}"
247+
fi
230248
echo "Framework: $framework"
231249
FRAMEWORK_FLAGS+=("--framework=$framework")
232250
fi
233251
}
234252

235-
append_framework_flag "ON" "$FRAMEWORK_EXECUTORCH"
236-
append_framework_flag "$COREML" "$FRAMEWORK_BACKEND_COREML"
237-
append_framework_flag "$MPS" "$FRAMEWORK_BACKEND_MPS"
238-
append_framework_flag "$XNNPACK" "$FRAMEWORK_BACKEND_XNNPACK"
239-
append_framework_flag "$CUSTOM" "$FRAMEWORK_KERNELS_CUSTOM"
240-
append_framework_flag "$OPTIMIZED" "$FRAMEWORK_KERNELS_OPTIMIZED"
241-
append_framework_flag "$PORTABLE" "$FRAMEWORK_KERNELS_PORTABLE"
242-
append_framework_flag "$QUANTIZED" "$FRAMEWORK_KERNELS_QUANTIZED"
243-
244-
"$SOURCE_ROOT_DIR"/build/create_frameworks.sh "${FRAMEWORK_FLAGS[@]}"
253+
for mode in "${MODES[@]}"; do
254+
FRAMEWORK_FLAGS=()
255+
for platform in "${PLATFORMS[@]}"; do
256+
echo "Directory: $platform/$mode"
257+
FRAMEWORK_FLAGS+=("--directory=$platform/$mode")
258+
done
259+
260+
append_framework_flag "ON" "$FRAMEWORK_EXECUTORCH" "$mode"
261+
append_framework_flag "$COREML" "$FRAMEWORK_BACKEND_COREML" "$mode"
262+
append_framework_flag "$MPS" "$FRAMEWORK_BACKEND_MPS" "$mode"
263+
append_framework_flag "$XNNPACK" "$FRAMEWORK_BACKEND_XNNPACK" "$mode"
264+
append_framework_flag "$CUSTOM" "$FRAMEWORK_KERNELS_CUSTOM" "$mode"
265+
append_framework_flag "$OPTIMIZED" "$FRAMEWORK_KERNELS_OPTIMIZED" "$mode"
266+
append_framework_flag "$PORTABLE" "$FRAMEWORK_KERNELS_PORTABLE" "$mode"
267+
append_framework_flag "$QUANTIZED" "$FRAMEWORK_KERNELS_QUANTIZED" "$mode"
268+
269+
"$SOURCE_ROOT_DIR"/build/create_frameworks.sh "${FRAMEWORK_FLAGS[@]}"
270+
done
245271

246272
echo "Cleaning up"
247273

0 commit comments

Comments
 (0)