@@ -44,11 +44,17 @@ cmake_minimum_required(VERSION 3.19)
44
44
project (executorch )
45
45
include (build /Utils.cmake )
46
46
47
+ set (CMAKE_EXPORT_COMPILE_COMMANDS ON )
48
+
49
+ if (NOT CMAKE_CXX_STANDARD )
50
+ set (CMAKE_CXX_STANDARD 17 )
51
+ endif ()
52
+
47
53
if (NOT CMAKE_BUILD_TYPE )
48
54
set (CMAKE_BUILD_TYPE Debug )
49
55
endif ()
50
- # -Os: Optimize for size -ffunction-sections -fdata-sections: breaks function
51
- # and data into sections so they can be properly gc'd -s: strip symbols
56
+ # -Os: Optimize for size. -ffunction-sections -fdata-sections: breaks function
57
+ # and data into sections so they can be properly gc'd. -s: strip symbols
52
58
set (CMAKE_CXX_FLAGS_RELEASE
53
59
"-Os -ffunction-sections -fdata-sections -s -fno-exceptions -fno-rtti" )
54
60
set (CMAKE_CXX_FLAGS_DEBUG "-O0 -g" )
@@ -60,8 +66,7 @@ set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g")
60
66
option (
61
67
REGISTER_EXAMPLE_CUSTOM_OP
62
68
"Register whether custom op 1 (my_ops::mul3) or custom op 2 (my_ops::mul4) \
63
- or no custom op at all."
64
- OFF )
69
+ or no custom op at all." OFF )
65
70
66
71
# Option to register quantized ops with quantized kernels. See
67
72
# kernels/quantized/CMakeLists.txt
@@ -86,12 +91,6 @@ endif()
86
91
option (EXECUTORCH_BUILD_XNNPACK
87
92
"Build xnn_executor_runner which depends on XNNPACK" OFF )
88
93
89
- set (CMAKE_EXPORT_COMPILE_COMMANDS ON )
90
-
91
- if (NOT CMAKE_CXX_STANDARD )
92
- set (CMAKE_CXX_STANDARD 17 )
93
- endif ()
94
-
95
94
if (NOT BUCK2 )
96
95
set (BUCK2 buck2 )
97
96
endif ()
@@ -125,11 +124,68 @@ endif()
125
124
message (STATUS "executorch: Using sources file ${EXECUTORCH_SRCS_FILE} " )
126
125
include (${EXECUTORCH_SRCS_FILE} )
127
126
127
+ #
128
+ # Modify default options when cross-compiling.
129
+ #
130
+ # The intent is for the EXECUTORCH_BUILD_HOST_TARGETS option to affect the
131
+ # default ON/OFF values of host targets around the tree. This way, a user can
132
+ # disable EXECUTORCH_BUILD_HOST_TARGETS to disable all host targets, and then
133
+ # optionally re-enable some of those targets. Or they could leave
134
+ # EXECUTORCH_BUILD_HOST_TARGETS enabled and then optionally disable any given
135
+ # host target.
136
+ #
137
+ # We can then use various cross-compilation hints to set the default value of
138
+ # EXECUTORCH_BUILD_HOST_TARGETS, which can still be overridden if desired.
139
+ #
140
+
141
+ # _default_build_host_targets: The default value for the
142
+ # EXECUTORCH_BUILD_HOST_TARGETS option.
143
+ set (_default_build_host_targets ON )
144
+ if (CMAKE_TOOLCHAIN_FILE MATCHES ".*ios\. toolchain\. cmake$" )
145
+ # TODO(dbort): Refactor toolchain-specific matching. Ideally this file would
146
+ # not know about specific targets, and work with more abstract concepts like
147
+ # "is cross compiling".
148
+ set (_default_build_host_targets OFF )
149
+ endif ()
150
+
151
+ # EXECUTORCH_BUILD_HOST_TARGETS: Option to control the building of host-only
152
+ # tools like `flatc`, along with example executables like `executor_runner` and
153
+ # libraries that it uses, like `gflags`. Disabling this can be helpful when
154
+ # cross-compiling, but some required tools that would have been built need to be
155
+ # provided directly (via, for example, FLATC_EXECUTABLE).
156
+ option (EXECUTORCH_BUILD_HOST_TARGETS "Build host-only targets."
157
+ "${_default_build_host_targets} " )
158
+ # Unset this since it's easy to confuse with _default_host_target_option.
159
+ unset (_default_build_host_targets )
160
+
161
+ # _default_host_target_option: EXECUTORCH_BUILD_* options that guard host tools
162
+ # and libraries should use this as their default value.
163
+ if (EXECUTORCH_BUILD_HOST_TARGETS )
164
+ set (_default_host_target_option ON )
165
+ else ()
166
+ set (_default_host_target_option OFF )
167
+ endif ()
168
+
128
169
#
129
170
# flatc: Flatbuffer commandline tool to generate .h files from .fbs files
130
171
#
131
172
132
- if (NOT FLATC_EXECUTABLE )
173
+ if (FLATC_EXECUTABLE )
174
+ # A `flatc` is provided, so no need to build another one.
175
+ set (_default_build_flatc OFF )
176
+ else ()
177
+ set (_default_build_flatc "${_default_host_target_option} " )
178
+ endif ()
179
+ option (EXECUTORCH_BUILD_FLATC "Build the flatc executable."
180
+ "${_default_build_flatc} " )
181
+ if (EXECUTORCH_BUILD_FLATC )
182
+ if (FLATC_EXECUTABLE )
183
+ # We could ignore this, but it could lead to confusion about which `flatc`
184
+ # is actually being used.
185
+ message (
186
+ FATAL_ERROR "May not set both EXECUTORCH_BUILD_FLATC and FLATC_EXECUTABLE"
187
+ )
188
+ endif ()
133
189
set (FLATC_EXECUTABLE flatc )
134
190
option (FLATBUFFERS_BUILD_FLATC "" ON )
135
191
option (FLATBUFFERS_BUILD_FLATHASH "" OFF )
@@ -138,12 +194,13 @@ if(NOT FLATC_EXECUTABLE)
138
194
option (FLATBUFFERS_INSTALL "" OFF )
139
195
add_subdirectory (third-party/flatbuffers )
140
196
endif ()
141
-
142
- #
143
- # gflags: Commandline flag libgrary
144
- #
145
-
146
- add_subdirectory (third-party/gflags )
197
+ if (NOT FLATC_EXECUTABLE )
198
+ message (
199
+ FATAL_ERROR
200
+ "FLATC_EXECUTABLE must be set when EXECUTORCH_BUILD_FLATC is disabled. "
201
+ "Note that EXECUTORCH_BUILD_FLATC may be disabled implicitly when "
202
+ "cross-compiling or when EXECUTORCH_BUILD_HOST_TARGETS is disabled." )
203
+ endif ()
147
204
148
205
#
149
206
# program_schema: Generated .h files from schema/*.fbs inputs
@@ -175,27 +232,42 @@ endif()
175
232
# operators necessary for the models that will run.
176
233
#
177
234
add_subdirectory (${CMAKE_CURRENT_SOURCE_DIR} /kernels/portable )
178
- set (_libs executorch portable_ops_lib gflags )
179
235
180
- # Generate custom_ops_lib based on REGISTER_EXAMPLE_CUSTOM_OP
181
- if (REGISTER_EXAMPLE_CUSTOM_OP EQUAL 1 OR REGISTER_EXAMPLE_CUSTOM_OP EQUAL 2 )
182
- add_subdirectory (${CMAKE_CURRENT_SOURCE_DIR} /examples/custom_ops )
183
- list (APPEND _libs custom_ops_lib )
236
+ #
237
+ # gflags: Commandline flag host library.
238
+ #
239
+ option (EXECUTORCH_BUILD_GFLAGS "Build the gflags library."
240
+ "${_default_host_target_option} " )
241
+ if (EXECUTORCH_BUILD_GFLAGS )
242
+ add_subdirectory (third-party/gflags )
184
243
endif ()
185
244
186
- # Generate lib to register quantized ops
187
- if (REGISTER_QUANTIZED_OPS )
188
- add_subdirectory (${CMAKE_CURRENT_SOURCE_DIR} /kernels/quantized )
189
- list (APPEND _libs quantized_ops_lib )
190
- endif ()
245
+ #
246
+ # executor_runner: Host tool that demonstrates program execution.
247
+ #
248
+ option (EXECUTORCH_BUILD_EXECUTOR_RUNNER "Build the executor_runner executable"
249
+ "${_default_host_target_option} " )
250
+ if (EXECUTORCH_BUILD_EXECUTOR_RUNNER )
251
+ # Baseline libraries that executor_runner will link against.
252
+ set (_executor_runner_libs executorch portable_ops_lib gflags )
253
+
254
+ # Generate custom_ops_lib based on REGISTER_EXAMPLE_CUSTOM_OP
255
+ if (REGISTER_EXAMPLE_CUSTOM_OP EQUAL 1 OR REGISTER_EXAMPLE_CUSTOM_OP EQUAL 2 )
256
+ add_subdirectory (${CMAKE_CURRENT_SOURCE_DIR} /examples/custom_ops )
257
+ list (APPEND _executor_runner_libs custom_ops_lib )
258
+ endif ()
259
+
260
+ # Generate lib to register quantized ops
261
+ if (REGISTER_QUANTIZED_OPS )
262
+ add_subdirectory (${CMAKE_CURRENT_SOURCE_DIR} /kernels/quantized )
263
+ list (APPEND _executor_runner_libs quantized_ops_lib )
264
+ endif ()
191
265
192
- # ios can only build library but not binary
193
- if (NOT CMAKE_TOOLCHAIN_FILE MATCHES ".*ios\. toolchain\. cmake$" )
194
266
add_executable (executor_runner ${_executor_runner__srcs} )
195
267
if (CMAKE_BUILD_TYPE EQUAL "RELEASE" )
196
268
target_link_options (executor_runner PRIVATE "LINKER:--gc-sections" )
197
269
endif ()
198
- target_link_libraries (executor_runner ${_libs } )
270
+ target_link_libraries (executor_runner ${_executor_runner_libs } )
199
271
target_compile_options (executor_runner PUBLIC ${_common_compile_options} )
200
272
endif ()
201
273
0 commit comments