1# 2# Copyright (c) 2023 Apple Inc. All rights reserved. 3# Provided subject to the LICENSE file in the top level directory. 4# 5 6# 7# mps_executor_runner: Host tool that demonstrates program execution using 8# MPSBackend. 9# 10 11cmake_minimum_required(VERSION 3.19) 12 13project(mps_runner_example) 14 15set(CMAKE_EXPORT_COMPILE_COMMANDS ON) 16 17if(NOT CMAKE_CXX_STANDARD) 18 set(CMAKE_CXX_STANDARD 17) 19endif() 20 21if(NOT FLATC_EXECUTABLE) 22 set(FLATC_EXECUTABLE flatc) 23endif() 24 25# Source root directory for executorch. 26if(NOT EXECUTORCH_ROOT) 27 set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..) 28endif() 29 30include(${EXECUTORCH_ROOT}/build/Utils.cmake) 31 32if(NOT PYTHON_EXECUTABLE) 33 resolve_python_executable() 34endif() 35 36add_compile_options("-Wall" "-Werror") 37 38include(${EXECUTORCH_ROOT}/build/Utils.cmake) 39 40set(_common_compile_options -Wno-deprecated-declarations -fPIC 41 -DET_EVENT_TRACER_ENABLED 42) 43 44# Let files say "include <executorch/path/to/header.h>". 45set(_common_include_directories ${EXECUTORCH_ROOT}/..) 46 47# Find prebuilt libraries. executorch package should contain portable_ops_lib, 48# etdump, bundled_program. 49find_package(executorch CONFIG REQUIRED) 50target_include_directories(executorch INTERFACE ${_common_include_directories}) 51target_compile_options(executorch INTERFACE ${_common_compile_options}) 52 53find_package( 54 gflags REQUIRED PATHS ${CMAKE_CURRENT_BINARY_DIR}/../../../third-party 55) 56 57# ios can only build library but not binary 58if(NOT CMAKE_TOOLCHAIN_FILE MATCHES ".*(iOS|ios\.toolchain)\.cmake$") 59 # 60 # mps_executor_runner: Like executor_runner but with MPS, the binary will be 61 # at ${CMAKE_BINARY_DIR}/examples/apple/executor_runner/mps 62 # 63 64 # portable_ops_lib 65 include(${EXECUTORCH_ROOT}/build/Utils.cmake) 66 include(${EXECUTORCH_ROOT}/build/Codegen.cmake) 67 gen_selected_ops(LIB_NAME "mps_portable_ops_lib" INCLUDE_ALL_OPS "ON") 68 generate_bindings_for_kernels( 69 LIB_NAME "mps_portable_ops_lib" FUNCTIONS_YAML 70 ${EXECUTORCH_ROOT}/kernels/portable/functions.yaml 71 ) 72 gen_operators_lib( 73 LIB_NAME "mps_portable_ops_lib" KERNEL_LIBS portable_kernels DEPS 74 executorch 75 ) 76 77 set(mps_executor_runner_libs 78 "-framework Foundation" "-weak_framework MetalPerformanceShaders" 79 "-weak_framework MetalPerformanceShadersGraph" "-weak_framework Metal" 80 ) 81 82 # 83 # The `_<target>_srcs` lists are defined by including ${EXECUTORCH_SRCS_FILE}. 84 # 85 set(EXECUTORCH_SRCS_FILE 86 "${CMAKE_CURRENT_BINARY_DIR}/../../../executorch_srcs.cmake" 87 ) 88 89 extract_sources(${EXECUTORCH_SRCS_FILE}) 90 91 set(_mps_schema_headers ${CMAKE_BINARY_DIR}/../../../schema/include/) 92 include(${EXECUTORCH_SRCS_FILE}) 93 target_include_directories( 94 bundled_program 95 INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/../../../devtools/include 96 ${CMAKE_CURRENT_BINARY_DIR}/../../../devtools/bundled_program 97 ${EXECUTORCH_ROOT}/third-party/flatbuffers/include 98 ${EXECUTORCH_ROOT}/third-party/flatcc/include 99 ${_mps_schema_headers} 100 ) 101 list(TRANSFORM _mps_executor_runner__srcs PREPEND "${EXECUTORCH_ROOT}/") 102 add_executable(mps_executor_runner ${_mps_executor_runner__srcs}) 103 104 if(CMAKE_BUILD_TYPE MATCHES "Debug") 105 set(FLATCC_LIB flatccrt_d) 106 else() 107 set(FLATCC_LIB flatccrt) 108 endif() 109 110 if(CMAKE_BUILD_TYPE MATCHES "Debug") 111 target_link_options(mps_executor_runner PUBLIC -fsanitize=undefined) 112 endif() 113 114 target_link_libraries( 115 mps_executor_runner 116 bundled_program 117 executorch 118 gflags 119 etdump 120 ${FLATCC_LIB} 121 mpsdelegate 122 mps_portable_ops_lib 123 ${mps_executor_runner_libs} 124 ) 125 target_compile_options(mps_executor_runner PUBLIC ${_common_compile_options}) 126endif() 127