xref: /aosp_15_r20/external/executorch/examples/selective_build/CMakeLists.txt (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1# Copyright (c) Meta Platforms, Inc. and affiliates.
2# All rights reserved.
3#
4# This source code is licensed under the BSD-style license found in the
5# LICENSE file in the root directory of this source tree.
6
7#
8# Simple CMake build system for selective build demo.
9#
10# ### Editing this file ###
11#
12# This file should be formatted with
13# ~~~
14# cmake-format -i CMakeLists.txt
15# ~~~
16# It should also be cmake-lint clean.
17#
18cmake_minimum_required(VERSION 3.19)
19project(selective_build_example)
20
21set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
22set(TORCH_ROOT ${EXECUTORCH_ROOT}/third-party/pytorch)
23
24include(${EXECUTORCH_ROOT}/build/Utils.cmake)
25include(${EXECUTORCH_ROOT}/build/Codegen.cmake)
26
27if(NOT PYTHON_EXECUTABLE)
28  resolve_python_executable()
29endif()
30
31if(NOT CMAKE_CXX_STANDARD)
32  set(CMAKE_CXX_STANDARD 17)
33  # Can't set to 11 due to executor_runner.cpp make_unique
34endif()
35
36set(_common_compile_options -Wno-deprecated-declarations -fPIC)
37
38# Let files say "include <executorch/path/to/header.h>".
39set(_common_include_directories ${EXECUTORCH_ROOT}/..)
40
41find_package(executorch CONFIG REQUIRED)
42find_package(
43  gflags REQUIRED PATHS ${CMAKE_CURRENT_BINARY_DIR}/../../third-party
44)
45
46target_include_directories(executorch INTERFACE ${_common_include_directories})
47
48# ------------------------------ OPTIONS BEGIN -------------------------------
49
50# Option to register ops from yaml file
51option(EXECUTORCH_SELECT_OPS_YAML "Register all the ops from a given yaml file"
52       OFF
53)
54
55# Option to register op list
56option(EXECUTORCH_SELECT_OPS_LIST "Register a list of ops, separated by comma"
57       OFF
58)
59
60# Selective build options.
61option(EXECUTORCH_SELECT_ALL_OPS
62       "Whether to register all ops defined in portable kernel library." OFF
63)
64# ------------------------------- OPTIONS END --------------------------------
65
66#
67# The `_<target>_srcs` lists are defined by including ${EXECUTORCH_SRCS_FILE}.
68#
69set(EXECUTORCH_SRCS_FILE
70    "${CMAKE_CURRENT_BINARY_DIR}/../../executorch_srcs.cmake"
71)
72
73extract_sources(${EXECUTORCH_SRCS_FILE})
74
75include(${EXECUTORCH_SRCS_FILE})
76
77#
78# select_build_lib: C++ library to register selected ops in custom kernel
79# library
80#
81set(_kernel_lib)
82if(EXECUTORCH_SELECT_OPS_YAML)
83  set(_custom_ops_yaml
84      ${EXECUTORCH_ROOT}/examples/portable/custom_ops/custom_ops.yaml
85  )
86  set(kernel_sources
87      ${EXECUTORCH_ROOT}/examples/portable/custom_ops/custom_ops_1_out.cpp
88      ${EXECUTORCH_ROOT}/examples/portable/custom_ops/custom_ops_2_out.cpp
89  )
90  #
91  # custom_kernels: C++ kernel implementations of custom ops
92  #
93  add_library(custom_kernels ${kernel_sources})
94  target_link_libraries(custom_kernels PRIVATE executorch)
95  target_compile_options(custom_kernels PUBLIC ${_common_compile_options})
96
97  list(APPEND _kernel_lib custom_kernels)
98else()
99  list(APPEND _kernel_lib portable_kernels)
100endif()
101
102gen_selected_ops(
103  LIB_NAME
104  "select_build_lib"
105  OPS_SCHEMA_YAML
106  "${_custom_ops_yaml}"
107  ROOT_OPS
108  "${EXECUTORCH_SELECT_OPS_LIST}"
109  INCLUDE_ALL_OPS
110  "${EXECUTORCH_SELECT_ALL_OPS}"
111)
112
113generate_bindings_for_kernels(
114  LIB_NAME "select_build_lib" FUNCTIONS_YAML
115  ${EXECUTORCH_ROOT}/kernels/portable/functions.yaml CUSTOM_OPS_YAML
116  "${_custom_ops_yaml}"
117)
118
119gen_operators_lib(
120  LIB_NAME "select_build_lib" KERNEL_LIBS ${_kernel_lib} DEPS executorch
121)
122
123list(TRANSFORM _executor_runner__srcs PREPEND "${EXECUTORCH_ROOT}/")
124
125#
126# selective_build_test: test binary to allow different operator libraries to
127# link to
128#
129add_executable(selective_build_test ${_executor_runner__srcs})
130if(CMAKE_BUILD_TYPE EQUAL "Release")
131  target_link_options(selective_build_test PRIVATE "LINKER:--gc-sections")
132endif()
133target_link_libraries(
134  selective_build_test PRIVATE executorch gflags select_build_lib
135)
136target_link_options_shared_lib(select_build_lib)
137target_link_options_shared_lib(executorch)
138target_compile_options(selective_build_test PUBLIC ${_common_compile_options})
139
140# Print all summary
141executorch_print_configuration_summary()
142