Skip to content

Commit e19d48f

Browse files
shoumikhinfacebook-github-bot
authored andcommitted
Build Mac frameworks. (#3429)
Summary: Pull Request resolved: #3429 Reviewed By: cccclai Differential Revision: D56778448 fbshipit-source-id: 4f6c1cf063eab29cf64a559e0a23afec245c814f
1 parent d0535fa commit e19d48f

9 files changed

Lines changed: 31 additions & 22 deletions

File tree

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,6 @@
6868
[submodule "examples/models/llama2/third-party/abseil-cpp"]
6969
path = examples/models/llama2/third-party/abseil-cpp
7070
url = https://github.com/abseil/abseil-cpp.git
71+
[submodule "third-party/ios-cmake"]
72+
path = third-party/ios-cmake
73+
url = https://github.com/leetal/ios-cmake

backends/apple/mps/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ target_link_libraries(
8484

8585
target_link_options_shared_lib(mpsdelegate)
8686
target_compile_options(mpsdelegate PUBLIC ${_common_compile_options})
87+
target_compile_options(mpsdelegate PRIVATE "-fno-objc-arc")
8788

8889
install(
8990
TARGETS mpsdelegate

backends/qualcomm/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ if(NOT DEFINED QNN_SDK_ROOT)
2727
FATAL_ERROR
2828
"Please define QNN_SDK_ROOT, e.g. cmake <..> -DQNN_SDK_ROOT=<...>"
2929
)
30-
elseif(CMAKE_TOOLCHAIN_FILE MATCHES ".*ios\.toolchain\.cmake$")
30+
elseif(CMAKE_TOOLCHAIN_FILE MATCHES ".*(iOS|ios\.toolchain)\.cmake$")
3131
message(FATAL_ERROR "ios is not supported by Qualcomm AI Engine Direct")
3232
endif()
3333

backends/vulkan/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ target_link_options_shared_lib(vulkan_backend)
130130

131131
# Executor Runner
132132

133-
if(NOT CMAKE_TOOLCHAIN_FILE MATCHES ".*iOS\.cmake$")
133+
if(NOT CMAKE_TOOLCHAIN_FILE MATCHES ".*(iOS|ios\.toolchain)\.cmake$")
134134
set(VULKAN_RUNNER_SRCS ${_executor_runner__srcs})
135135
list(TRANSFORM VULKAN_RUNNER_SRCS PREPEND "${EXECUTORCH_ROOT}/")
136136

backends/xnnpack/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ target_link_options_shared_lib(xnnpack_backend)
106106
list(APPEND xnn_executor_runner_libs xnnpack_backend)
107107

108108
# ios can only build library but not binary
109-
if(NOT CMAKE_TOOLCHAIN_FILE MATCHES ".*iOS\.cmake$")
109+
if(NOT CMAKE_TOOLCHAIN_FILE MATCHES ".*(iOS|ios\.toolchain)\.cmake$")
110110
#
111111
# xnn_executor_runner: Like executor_runner but with XNNPACK, the binary will
112112
# be at ${CMAKE_BINARY_DIR}/backends/xnnpack

build/build_apple_frameworks.sh

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77

88
set -euo pipefail
99

10-
PLATFORMS=("iphoneos" "iphonesimulator")
11-
PLATFORM_FLAGS=("OS" "SIMULATOR")
10+
PLATFORMS=("ios" "simulator" "macos")
11+
PLATFORM_FLAGS=("OS64" "SIMULATORARM64" "MAC_ARM64")
12+
PLATFORM_TARGET=("17.0" "17.0" "10.15")
1213
SOURCE_ROOT_DIR=""
1314
OUTPUT="cmake-out"
1415
MODE="Release"
1516
TOOLCHAIN=""
16-
BUCK2="buck2"
17+
BUCK2=$(which buck2)
1718
PYTHON=$(which python3)
18-
FLATC="flatc"
19-
IOS_DEPLOYMENT_TARGET="17.0"
19+
FLATC=$(which flatc)
2020
COREML=OFF
2121
CUSTOM=OFF
2222
MPS=OFF
@@ -42,10 +42,10 @@ usage() {
4242
echo "Options:"
4343
echo " --output=DIR Output directory. Default: 'cmake-out'"
4444
echo " --Debug Use Debug build mode. Default: 'Release'"
45-
echo " --toolchain=FILE Cmake toolchain file. Default: '\$SOURCE_ROOT_DIR/third-party/pytorch/cmake/iOS.cmake'"
46-
echo " --buck2=FILE Buck2 executable path. Default: '/tmp/buck2'"
45+
echo " --toolchain=FILE Cmake toolchain file. Default: '\$SOURCE_ROOT_DIR/third-party/ios-cmake/ios.toolchain.cmake'"
46+
echo " --buck2=FILE Buck2 executable path. Default: Path of buck2 found in the current \$PATH"
4747
echo " --python=FILE Python executable path. Default: Path of python3 found in the current \$PATH"
48-
echo " --flatc=FILE FlatBuffers Compiler executable path. Default: '\$SOURCE_ROOT_DIR/third-party/flatbuffers/cmake-out/flatc'"
48+
echo " --flatc=FILE FlatBuffers Compiler executable path. Default: Path of flatc found in the current \$PATH"
4949
echo " --coreml Include this flag to build the Core ML backend."
5050
echo " --custom Include this flag to build the Custom backend."
5151
echo " --mps Include this flag to build the Metal Performance Shaders backend."
@@ -68,7 +68,6 @@ for arg in "$@"; do
6868
--buck2=*) BUCK2="${arg#*=}" ;;
6969
--python=*) PYTHON="${arg#*=}" ;;
7070
--flatc=*) FLATC="${arg#*=}" ;;
71-
--ios-deployment-target=*) IOS_DEPLOYMENT_TARGET="${arg#*=}" ;;
7271
--coreml) COREML=ON ;;
7372
--custom) CUSTOM=ON ;;
7473
--mps) MPS=ON ;;
@@ -92,14 +91,10 @@ if [[ -z "$SOURCE_ROOT_DIR" ]]; then
9291
fi
9392

9493
if [[ -z "$TOOLCHAIN" ]]; then
95-
TOOLCHAIN="$SOURCE_ROOT_DIR/third-party/pytorch/cmake/iOS.cmake"
94+
TOOLCHAIN="$SOURCE_ROOT_DIR/third-party/ios-cmake/ios.toolchain.cmake"
9695
fi
9796
[[ -f "$TOOLCHAIN" ]] || { echo >&2 "Toolchain file $TOOLCHAIN does not exist."; exit 1; }
9897

99-
if [[ -z "$FLATC" ]]; then
100-
FLATC="$SOURCE_ROOT_DIR/third-party/flatbuffers/cmake-out/flatc"
101-
fi
102-
10398
check_command() {
10499
command -v "$1" >/dev/null 2>&1 || { echo >&2 "$1 is not installed"; exit 1; }
105100
}
@@ -117,6 +112,7 @@ rm -rf "$OUTPUT" && mkdir -p "$OUTPUT" && cd "$OUTPUT" || exit 1
117112
cmake_build() {
118113
local platform=$1
119114
local platform_flag=$2
115+
local platform_target=$3
120116
echo "Building for $platform with flag $platform_flag"
121117
mkdir "$platform" && cd "$platform" || exit 1
122118
cmake "$SOURCE_ROOT_DIR" -G Xcode \
@@ -132,20 +128,23 @@ cmake_build() {
132128
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
133129
-DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \
134130
-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY="$(pwd)" \
135-
-DIOS_DEPLOYMENT_TARGET="$IOS_DEPLOYMENT_TARGET" \
136131
-DEXECUTORCH_BUILD_COREML=$COREML \
137132
-DEXECUTORCH_BUILD_CUSTOM=$CUSTOM \
138133
-DEXECUTORCH_BUILD_MPS=$MPS \
139134
-DEXECUTORCH_BUILD_OPTIMIZED=$OPTIMIZED \
140135
-DEXECUTORCH_BUILD_QUANTIZED=$QUANTIZED \
141136
-DEXECUTORCH_BUILD_XNNPACK=$XNNPACK \
142-
${platform_flag:+-DIOS_PLATFORM=$platform_flag}
143-
cmake --build . --config $MODE
137+
${platform_flag:+-DPLATFORM=$platform_flag} \
138+
${platform_target:+-DDEPLOYMENT_TARGET=$platform_target} \
139+
--log-level=VERBOSE
140+
cmake --build . \
141+
--config $MODE \
142+
--verbose
144143
cd ..
145144
}
146145

147146
for index in ${!PLATFORMS[*]}; do
148-
cmake_build "${PLATFORMS[$index]}" "${PLATFORM_FLAGS[$index]}"
147+
cmake_build "${PLATFORMS[$index]}" "${PLATFORM_FLAGS[$index]}" "${PLATFORM_TARGET[$index]}"
149148
done
150149

151150
echo "Exporting headers"

build/test_ios.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ say "Downloading Labels"
9797
curl https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt \
9898
-o "$APP_PATH/Resources/Models/MobileNet/imagenet_classes.txt"
9999

100+
say "Building Frameworks"
101+
102+
./build/build_apple_frameworks.sh --buck2="$(realpath $BUCK2)" --coreml --custom --mps --optimized --portable --quantized --xnnpack
103+
mv cmake-out "$APP_PATH/Frameworks"
104+
100105
say "Creating Simulator"
101106

102107
xcrun simctl create "$SIMULATOR_NAME" "iPhone 15"

examples/apple/mps/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ find_package(
6060
)
6161

6262
# ios can only build library but not binary
63-
if(NOT CMAKE_TOOLCHAIN_FILE MATCHES ".*iOS\.cmake$")
63+
if(NOT CMAKE_TOOLCHAIN_FILE MATCHES ".*(iOS|ios\.toolchain)\.cmake$")
6464
#
6565
# mps_executor_runner: Like executor_runner but with MPS, the binary will be
6666
# at ${CMAKE_BINARY_DIR}/examples/apple/executor_runner/mps

third-party/ios-cmake

Submodule ios-cmake added at 06465b2

0 commit comments

Comments
 (0)