Skip to content

Commit e7bd5e8

Browse files
committed
update apple build script
1 parent ebda84d commit e7bd5e8

File tree

3 files changed

+81
-167
lines changed

3 files changed

+81
-167
lines changed

.github/workflows/apple-perf.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,7 @@ jobs:
386386
echo "::endgroup::"
387387
388388
echo "::group::Build ExecuTorch iOS frameworks"
389-
PYTHON_EXECUTABLE=python ${CONDA_RUN} --no-capture-output \
390-
scripts/build_apple_frameworks.sh --Release --Debug --coreml --custom --mps --optimized --portable --quantized --xnnpack
389+
PYTHON_EXECUTABLE=python ${CONDA_RUN} --no-capture-output scripts/build_apple_frameworks.sh
391390
echo "::endgroup::"
392391
393392
# NB: Although exported models can be copied to this directory and bundled together with the

.github/workflows/apple.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,7 @@ jobs:
173173
backends/apple/mps/install_requirements.sh
174174
175175
# Build iOS Frameworks
176-
PYTHON_EXECUTABLE=python ${CONDA_RUN} --no-capture-output \
177-
scripts/build_apple_frameworks.sh --Release --Debug --coreml --custom --mps --optimized --portable --quantized --xnnpack
176+
PYTHON_EXECUTABLE=python ${CONDA_RUN} --no-capture-output scripts/build_apple_frameworks.sh
178177
179178
# Bundle iOS Frameworks
180179
for FRAMEWORK in "${FRAMEWORKS[@]}"; do (
@@ -314,8 +313,7 @@ jobs:
314313
echo "::endgroup::"
315314
316315
echo "::group::Build ExecuTorch iOS frameworks"
317-
PYTHON_EXECUTABLE=python ${CONDA_RUN} --no-capture-output \
318-
scripts/build_apple_frameworks.sh --Release --Debug --coreml --custom --mps --optimized --portable --quantized --xnnpack
316+
PYTHON_EXECUTABLE=python ${CONDA_RUN} --no-capture-output scripts/build_apple_frameworks.sh
319317
echo "::endgroup::"
320318
321319
echo "::group::Build ExecuTorch benchmark app"

scripts/build_apple_frameworks.sh

Lines changed: 78 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,13 @@
77

88
set -euxo pipefail
99

10-
SOURCE_ROOT_DIR=""
11-
OUTPUT="cmake-out"
12-
MODES=()
13-
TOOLCHAIN=""
14-
PYTHON=$(which python3)
15-
COREML=OFF
16-
CUSTOM=OFF
17-
MPS=OFF
18-
OPTIMIZED=OFF
19-
PORTABLE=OFF
20-
QUANTIZED=OFF
21-
XNNPACK=OFF
22-
HEADERS_PATH="include"
23-
24-
PLATFORMS=("ios" "simulator" "macos")
25-
PLATFORM_FLAGS=("OS64" "SIMULATORARM64" "MAC_ARM64")
26-
PLATFORM_TARGET=("17.0" "17.0" "10.15")
10+
MODES=("Release" "Debug")
11+
PRESETS=("ios" "ios-simulator" "macos")
12+
13+
SOURCE_ROOT_DIR=$(git rev-parse --show-toplevel)
14+
OUTPUT_DIR="${SOURCE_ROOT_DIR}/cmake-out"
15+
HEADERS_RELATIVE_PATH="include"
16+
HEADERS_ABSOLUTE_PATH="${OUTPUT_DIR}/${HEADERS_RELATIVE_PATH}"
2717

2818
FRAMEWORK_EXECUTORCH="executorch:\
2919
libexecutorch.a,\
@@ -33,7 +23,7 @@ libextension_data_loader.a,\
3323
libextension_flat_tensor.a,\
3424
libextension_module.a,\
3525
libextension_tensor.a,\
36-
:$HEADERS_PATH:ExecuTorch"
26+
:$HEADERS_RELATIVE_PATH:ExecuTorch"
3727

3828
FRAMEWORK_BACKEND_COREML="backend_coreml:\
3929
libcoreml_util.a,\
@@ -75,33 +65,30 @@ libquantized_ops_lib.a,\
7565
:"
7666

7767
usage() {
78-
echo "Usage: $0 [SOURCE_ROOT_DIR] [OPTIONS]"
68+
echo "Usage: $0 [OPTIONS]"
7969
echo "Build frameworks for Apple platforms."
80-
echo "SOURCE_ROOT_DIR defaults to the current directory if not provided."
8170
echo
8271
echo "Options:"
83-
echo " --output=DIR Output directory. Default: 'cmake-out'"
8472
echo " --Debug Build Debug version."
8573
echo " --Release Build Release version."
86-
echo " --toolchain=FILE CMake toolchain file. Default: '\$SOURCE_ROOT_DIR/third-party/ios-cmake/ios.toolchain.cmake'"
87-
echo " --python=FILE Python executable path. Default: Path of python3 in \$PATH"
88-
echo " --coreml Build the Core ML backend."
89-
echo " --custom Build the Custom kernels."
90-
echo " --mps Build the Metal Performance Shaders backend."
91-
echo " --optimized Build the Optimized kernels."
92-
echo " --portable Build the Portable kernels."
93-
echo " --quantized Build the Quantized kernels."
94-
echo " --xnnpack Build the XNNPACK backend."
9574
echo
96-
echo "Example:"
97-
echo " $0 /path/to/source/root --output=cmake-out --toolchain=/path/to/toolchain --python=/path/to/python3 --coreml --mps --xnnpack"
9875
exit 0
9976
}
10077

78+
deprecated_option() {
79+
local option_name="$1"
80+
local cmake_var="${2:-}"
81+
if [[ -n "$cmake_var" ]]; then
82+
echo -e "\033[31m[error] Flag '--${option_name}' is now ON by default and deprecated. To turn off that feature, use:\033[34m\n\n\tCMAKE_ARGS=\"-D${cmake_var}=OFF\" $0\n\033[0m"
83+
else
84+
echo -e "\033[31m[error] Flag '--${option_name}' is now ON by default and deprecated.\033[0m"
85+
fi
86+
exit 1
87+
}
88+
10189
for arg in "$@"; do
10290
case $arg in
10391
-h|--help) usage ;;
104-
--output=*) OUTPUT="${arg#*=}" ;;
10592
--Release)
10693
if [[ ! " ${MODES[*]:-} " =~ \bRelease\b ]]; then
10794
MODES+=("Release")
@@ -112,135 +99,60 @@ for arg in "$@"; do
11299
MODES+=("Debug")
113100
fi
114101
;;
115-
--toolchain=*) TOOLCHAIN="${arg#*=}" ;;
116-
--python=*) PYTHON="${arg#*=}" ;;
117-
--coreml) COREML=ON ;;
118-
--custom) CUSTOM=ON ;;
119-
--mps) MPS=ON ;;
120-
--optimized) OPTIMIZED=ON ;;
121-
--portable) PORTABLE=ON ;;
122-
--quantized) QUANTIZED=ON ;;
123-
--xnnpack) XNNPACK=ON ;;
102+
--coreml) deprecated_option "coreml" "EXECUTORCH_BUILD_COREML";;
103+
--custom) deprecated_option "custom" "EXECUTORCH_BUILD_KERNELS_CUSTOM" ;;
104+
--mps) deprecated_option "mps" "EXECUTORCH_BUILD_MPS" ;;
105+
--optimized) deprecated_option "optimized" "EXECUTORCH_BUILD_KERNELS_OPTIMIZED" ;;
106+
--portable) deprecated_option "portable" ;;
107+
--quantized) deprecated_option "quantized" "EXECUTORCH_BUILD_KERNELS_QUANTIZED" ;;
108+
--xnnpack) deprecated_option "xnnpack" "EXECUTORCH_BUILD_XNNPACK" ;;
124109
*)
125-
if [[ -z "$SOURCE_ROOT_DIR" ]]; then
126-
SOURCE_ROOT_DIR="$arg"
127-
else
128-
echo "Invalid argument: $arg"
129-
exit 1
130-
fi
131110
;;
132111
esac
133112
done
134113

135-
if [ ${#MODES[@]} -eq 0 ]; then
136-
MODES=("Release")
137-
fi
138-
139-
if [[ -z "$SOURCE_ROOT_DIR" ]]; then
140-
SOURCE_ROOT_DIR=$(pwd)
141-
fi
142-
143-
if [[ -z "$TOOLCHAIN" ]]; then
144-
TOOLCHAIN="$SOURCE_ROOT_DIR/third-party/ios-cmake/ios.toolchain.cmake"
145-
fi
146-
[[ -f "$TOOLCHAIN" ]] || { echo >&2 "Toolchain file $TOOLCHAIN does not exist."; exit 1; }
147-
148-
BUCK2=$("$PYTHON" "$SOURCE_ROOT_DIR/tools/cmake/resolve_buck.py" --cache_dir="$SOURCE_ROOT_DIR/buck2-bin")
114+
BUCK2=$(python3 "$SOURCE_ROOT_DIR/tools/cmake/resolve_buck.py" --cache_dir="$SOURCE_ROOT_DIR/buck2-bin")
149115

150116
if [[ "$BUCK2" == "buck2" ]]; then
151117
BUCK2=$(command -v buck2)
152118
fi
153119

154-
check_command() {
155-
if [[ "$1" == */* ]]; then
156-
if [[ ! -x "$1" ]]; then
157-
echo "Error: Command not found or not executable at '$1'" >&2
158-
exit 1
159-
fi
160-
else
161-
if ! command -v "$1" >/dev/null 2>&1; then
162-
echo "Error: Command '$1' not found in PATH" >&2
163-
exit 1
164-
fi
165-
fi
166-
}
167-
168-
check_command cmake
169-
check_command rsync
170-
check_command "$PYTHON"
171-
check_command "$BUCK2"
172-
173120
echo "Building libraries"
174121

175-
rm -rf "$OUTPUT" && mkdir -p "$OUTPUT" && cd "$OUTPUT" || exit 1
176-
177-
cmake_build() {
178-
local platform=$1
179-
local platform_flag=$2
180-
local platform_target=$3
181-
local mode=$4
182-
echo "Building for $platform ($mode) with flag $platform_flag"
183-
mkdir -p "$platform" && cd "$platform" || exit 1
184-
cmake "$SOURCE_ROOT_DIR" -G Xcode \
185-
-DCMAKE_BUILD_TYPE="$mode" \
186-
-DCMAKE_TOOLCHAIN_FILE="$TOOLCHAIN" \
187-
-DCMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LANGUAGE_STANDARD="c++17" \
188-
-DCMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY="libc++" \
189-
-DCMAKE_C_FLAGS="-ffile-prefix-map=$SOURCE_ROOT_DIR=/executorch -fdebug-prefix-map=$SOURCE_ROOT_DIR=/executorch" \
190-
-DCMAKE_CXX_FLAGS="-ffile-prefix-map=$SOURCE_ROOT_DIR=/executorch -fdebug-prefix-map=$SOURCE_ROOT_DIR=/executorch" \
191-
-DPYTHON_EXECUTABLE="$PYTHON" \
192-
-DEXECUTORCH_BUILD_COREML=$COREML \
193-
-DEXECUTORCH_BUILD_MPS=$MPS \
194-
-DEXECUTORCH_BUILD_XNNPACK=$XNNPACK \
195-
-DEXECUTORCH_XNNPACK_SHARED_WORKSPACE=ON \
196-
-DEXECUTORCH_BUILD_EXECUTOR_RUNNER=OFF \
197-
-DEXECUTORCH_BUILD_EXTENSION_APPLE=ON \
198-
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
199-
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
200-
-DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON \
201-
-DEXECUTORCH_BUILD_KERNELS_CUSTOM=$CUSTOM \
202-
-DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=$OPTIMIZED \
203-
-DEXECUTORCH_BUILD_KERNELS_QUANTIZED=$QUANTIZED \
204-
-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY="$(pwd)" \
205-
${platform_flag:+-DPLATFORM=$platform_flag} \
206-
${platform_target:+-DDEPLOYMENT_TARGET=$platform_target} \
207-
--log-level=VERBOSE
208-
cmake --build . \
209-
--config "$mode" \
210-
--verbose
211-
cd -
212-
}
213-
214-
for index in ${!PLATFORMS[*]}; do
122+
rm -rf "${OUTPUT_DIR}"
123+
for preset in "${PRESETS[@]}"; do
215124
for mode in "${MODES[@]}"; do
216-
cmake_build "${PLATFORMS[$index]}" "${PLATFORM_FLAGS[$index]}" "${PLATFORM_TARGET[$index]}" "$mode"
125+
output_dir="${OUTPUT_DIR}/${preset}"
126+
echo "Building preset ${preset} (${mode}) in ${output_dir}..."
127+
128+
# Do NOT add options here. Update the respective presets instead.
129+
cmake -S "${SOURCE_ROOT_DIR}" \
130+
-B "${output_dir}" \
131+
-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY="${output_dir}" \
132+
-DCMAKE_BUILD_TYPE="${mode}" \
133+
${CMAKE_ARGS:-} \
134+
--preset "${preset}"
135+
136+
cmake --build "${output_dir}" \
137+
--config "${mode}" \
138+
-j$(sysctl -n hw.ncpu)
217139
done
218140
done
219141

220142
echo "Exporting headers"
221143

222-
mkdir -p "$HEADERS_PATH"
144+
mkdir -p "$HEADERS_ABSOLUTE_PATH"
223145

224146
"$SOURCE_ROOT_DIR"/scripts/print_exported_headers.py --buck2=$(realpath "$BUCK2") --targets \
225147
//extension/module: \
226148
//extension/tensor: \
227-
| rsync -av --files-from=- "$SOURCE_ROOT_DIR" "$HEADERS_PATH/executorch"
228-
229-
# HACK: XCFrameworks don't appear to support exporting any build
230-
# options, but we need the following:
231-
# - runtime/core/portable/type/c10 reachable with `#include <c10/...>`
232-
# - exported -DC10_USING_CUSTOM_GENERATED_MACROS compiler flag
233-
# So, just patch our generated framework to do that.
234-
sed -i '' '1i\
235-
#define C10_USING_CUSTOM_GENERATED_MACROS
236-
' \
237-
"$HEADERS_PATH/executorch/runtime/core/portable_type/c10/c10/macros/Macros.h" \
238-
"$HEADERS_PATH/executorch/runtime/core/portable_type/c10/c10/macros/Export.h"
239-
240-
cp -r $HEADERS_PATH/executorch/runtime/core/portable_type/c10/c10 "$HEADERS_PATH/"
241-
242-
cp "$SOURCE_ROOT_DIR/extension/apple/ExecuTorch/Exported/"*.h "$HEADERS_PATH/executorch"
243-
cat > "$HEADERS_PATH/module.modulemap" << 'EOF'
149+
| rsync -av --files-from=- "$SOURCE_ROOT_DIR" "$HEADERS_ABSOLUTE_PATH/executorch"
150+
151+
cp -r $HEADERS_ABSOLUTE_PATH/executorch/runtime/core/portable_type/c10/c10 "$HEADERS_ABSOLUTE_PATH/"
152+
153+
cp "$SOURCE_ROOT_DIR/extension/apple/ExecuTorch/Exported/"*.h "$HEADERS_ABSOLUTE_PATH/executorch"
154+
155+
cat > "$HEADERS_ABSOLUTE_PATH/module.modulemap" << 'EOF'
244156
module ExecuTorch {
245157
umbrella header "ExecuTorch/ExecuTorch.h"
246158
export *
@@ -250,47 +162,52 @@ EOF
250162
echo "Creating frameworks"
251163

252164
append_framework_flag() {
253-
local flag="$1"
165+
local option="$1"
254166
local framework="$2"
255-
local mode="${3:-}"
256-
if [[ $flag == ON ]]; then
257-
if [[ -n "$mode" && "$mode" != "Release" ]]; then
167+
local mode="$3"
168+
169+
if [[ "${CMAKE_ARGS:-}" =~ "-D${option}=OFF" ]]; then
170+
echo "Skipping framework: ${framework}"
171+
return
172+
fi
173+
174+
if [[ -n "$mode" && "$mode" != "Release" ]]; then
258175
local name spec
259176
name=$(echo "$framework" | cut -d: -f1)
260177
spec=$(echo "$framework" | cut -d: -f2-)
261178
framework="${name}_$(echo "$mode" | tr '[:upper:]' '[:lower:]'):${spec}"
262-
fi
263-
echo "Framework: $framework"
264-
FRAMEWORK_FLAGS+=("--framework=$framework")
265179
fi
180+
echo "Adding framework: ${framework}"
181+
FRAMEWORK_FLAGS+=("--framework=$framework")
266182
}
267183

268184
for mode in "${MODES[@]}"; do
269185
FRAMEWORK_FLAGS=()
270-
for platform in "${PLATFORMS[@]}"; do
271-
echo "Directory: $platform/$mode"
272-
FRAMEWORK_FLAGS+=("--directory=$platform/$mode")
186+
for preset in "${PRESETS[@]}"; do
187+
echo "Framework directory: ${preset}/${mode}"
188+
FRAMEWORK_FLAGS+=("--directory=${preset}/${mode}")
273189
done
274190

275-
append_framework_flag "ON" "$FRAMEWORK_EXECUTORCH" "$mode"
276-
append_framework_flag "$COREML" "$FRAMEWORK_BACKEND_COREML" "$mode"
277-
append_framework_flag "$MPS" "$FRAMEWORK_BACKEND_MPS" "$mode"
278-
append_framework_flag "$XNNPACK" "$FRAMEWORK_BACKEND_XNNPACK" "$mode"
279-
append_framework_flag "$CUSTOM" "$FRAMEWORK_KERNELS_CUSTOM" "$mode"
280-
append_framework_flag "$OPTIMIZED" "$FRAMEWORK_KERNELS_OPTIMIZED" "$mode"
281-
append_framework_flag "$PORTABLE" "$FRAMEWORK_KERNELS_PORTABLE" "$mode"
282-
append_framework_flag "$QUANTIZED" "$FRAMEWORK_KERNELS_QUANTIZED" "$mode"
191+
append_framework_flag "" "$FRAMEWORK_EXECUTORCH" "$mode"
192+
append_framework_flag "EXECUTORCH_BUILD_COREML" "$FRAMEWORK_BACKEND_COREML" "$mode"
193+
append_framework_flag "EXECUTORCH_BUILD_MPS" "$FRAMEWORK_BACKEND_MPS" "$mode"
194+
append_framework_flag "EXECUTORCH_BUILD_XNNPACK" "$FRAMEWORK_BACKEND_XNNPACK" "$mode"
195+
append_framework_flag "EXECUTORCH_BUILD_KERNELS_CUSTOM" "$FRAMEWORK_KERNELS_CUSTOM" "$mode"
196+
append_framework_flag "EXECUTORCH_BUILD_KERNELS_OPTIMIZED" "$FRAMEWORK_KERNELS_OPTIMIZED" "$mode"
197+
append_framework_flag "" "$FRAMEWORK_KERNELS_PORTABLE" "$mode"
198+
append_framework_flag "EXECUTORCH_BUILD_KERNELS_QUANTIZED" "$FRAMEWORK_KERNELS_QUANTIZED" "$mode"
283199

200+
cd "${OUTPUT_DIR}"
284201
"$SOURCE_ROOT_DIR"/scripts/create_frameworks.sh "${FRAMEWORK_FLAGS[@]}"
285202
done
286203

287204
echo "Cleaning up"
288205

289-
for platform in "${PLATFORMS[@]}"; do
290-
rm -rf "$platform"
206+
for preset in "${PRESETS[@]}"; do
207+
rm -rf "${OUTPUT_DIR}/${preset}/$preset"
291208
done
292209

293-
rm -rf "$HEADERS_PATH"
210+
rm -rf "$HEADERS_ABSOLUTE_PATH"
294211

295212
echo "Running tests"
296213

0 commit comments

Comments
 (0)