From 36c21bd8237493fbc6a8d88327fd691ea1f3a7e5 Mon Sep 17 00:00:00 2001 From: Anthony Shoumikhin Date: Sat, 9 Mar 2024 10:59:18 -0800 Subject: [PATCH] Build Apple extensions with CMake. (#2336) Summary: Pull Request resolved: https://github.com/pytorch/executorch/pull/2336 bypass-github-export-checks Reviewed By: cccclai Differential Revision: D54711005 fbshipit-source-id: 88dbcba890080d584d0c3f23f2be2e235d2956e9 --- CMakeLists.txt | 4 ++ build/build_apple_frameworks.sh | 5 ++- extension/apple/CMakeLists.txt | 45 +++++++++++++++++++ .../ExecuTorch/Exported/module.modulemap | 6 +++ 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 extension/apple/CMakeLists.txt create mode 100644 extension/apple/ExecuTorch/Exported/module.modulemap diff --git a/CMakeLists.txt b/CMakeLists.txt index 185214c275..2803f5c4be 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -340,6 +340,10 @@ if(EXECUTORCH_BUILD_SDK) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/sdk) endif() +if(EXECUTORCH_BUILD_EXTENSION_APPLE) + add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/apple) +endif() + if(EXECUTORCH_BUILD_EXTENSION_DATA_LOADER) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/data_loader) endif() diff --git a/build/build_apple_frameworks.sh b/build/build_apple_frameworks.sh index c43ac8b568..30e36df136 100755 --- a/build/build_apple_frameworks.sh +++ b/build/build_apple_frameworks.sh @@ -22,7 +22,7 @@ MPS=OFF PORTABLE=OFF XNNPACK=OFF HEADERS_PATH="include" -EXECUTORCH_FRAMEWORK="executorch:libexecutorch.a,libextension_data_loader.a,libextension_module.a:$HEADERS_PATH" +EXECUTORCH_FRAMEWORK="executorch:libexecutorch.a,libextension_apple.a,libextension_data_loader.a,libextension_module.a:$HEADERS_PATH" COREML_FRAMEWORK="coreml_backend:libcoremldelegate.a:" MPS_FRAMEWORK="mps_backend:libmpsdelegate.a:" PORTABLE_FRAMEWORK="portable_backend:libportable_kernels.a,libportable_ops_lib.a:" @@ -115,6 +115,7 @@ cmake_build() { -DBUCK2="$BUCK2" \ -DPYTHON_EXECUTABLE="$PYTHON" \ -DFLATC_EXECUTABLE="$FLATC" \ + -DEXECUTORCH_BUILD_EXTENSION_APPLE=ON \ -DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \ -DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \ -DCMAKE_ARCHIVE_OUTPUT_DIRECTORY="$(pwd)" \ @@ -139,6 +140,8 @@ mkdir -p "$HEADERS_PATH" //extension/module: \ | rsync -av --files-from=- "$SOURCE_ROOT_DIR" "$HEADERS_PATH/executorch" +cp "$SOURCE_ROOT_DIR/extension/apple/ExecuTorch/Exported/"{*.h,*.modulemap} "$HEADERS_PATH" + echo "Creating frameworks" for platform in "${PLATFORMS[@]}"; do diff --git a/extension/apple/CMakeLists.txt b/extension/apple/CMakeLists.txt new file mode 100644 index 0000000000..db194270d3 --- /dev/null +++ b/extension/apple/CMakeLists.txt @@ -0,0 +1,45 @@ +# 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. + +# Please this file formatted by running: +# ~~~ +# cmake-format --first-comment-is-literal=True CMakeLists.txt +# ~~~ + +cmake_minimum_required(VERSION 3.19) + +# Source root directory for executorch. +if(NOT EXECUTORCH_ROOT) + set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..) +endif() + +add_library(extension_apple) + +set(EXPORTED_SOURCES + ExecuTorch/Exported/ExecuTorchLog.mm +) + +target_sources( + extension_apple PRIVATE + ${EXPORTED_SOURCES} +) + +target_include_directories( + extension_apple PUBLIC + ExecuTorch/Exported +) + +find_library(FOUNDATION_FRAMEWORK Foundation) +target_link_libraries(extension_apple + PRIVATE + executorch + ${FOUNDATION_FRAMEWORK} +) + +target_compile_options(extension_apple PUBLIC ${_common_compile_options}) +target_compile_options(extension_apple PRIVATE "-fobjc-arc") +target_compile_options(extension_apple PRIVATE "-fno-exceptions") +target_compile_options(extension_apple PRIVATE "-fno-rtti") diff --git a/extension/apple/ExecuTorch/Exported/module.modulemap b/extension/apple/ExecuTorch/Exported/module.modulemap new file mode 100644 index 0000000000..cb91d5d4d6 --- /dev/null +++ b/extension/apple/ExecuTorch/Exported/module.modulemap @@ -0,0 +1,6 @@ +module ExecuTorch { + umbrella header "ExecuTorch.h" + + export * + module * { export * } +} \ No newline at end of file