Skip to content

Commit 5714285

Browse files
dbortfacebook-github-bot
authored andcommitted
Add EXECUTORCH_BUILD_HOST_TARGETS option to guard host-only targets (#463)
Summary: Pull Request resolved: #463 When cross-compiling in CMake, users would like a way to avoid building host-only targets. Introduce a general mechanism to disable building targets like these. Also update the set of vars printed by Utils.cmake, and format/lint it. ghstack-source-id: 201888044 exported-using-ghexport Reviewed By: tarun292, cccclai Differential Revision: D49558464 fbshipit-source-id: 935ea7e10e02f7492bf2a343998ae38cc70f13ba
1 parent b6e182c commit 5714285

File tree

3 files changed

+134
-52
lines changed

3 files changed

+134
-52
lines changed

CMakeLists.txt

Lines changed: 102 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,17 @@ cmake_minimum_required(VERSION 3.19)
4444
project(executorch)
4545
include(build/Utils.cmake)
4646

47+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
48+
49+
if(NOT CMAKE_CXX_STANDARD)
50+
set(CMAKE_CXX_STANDARD 17)
51+
endif()
52+
4753
if(NOT CMAKE_BUILD_TYPE)
4854
set(CMAKE_BUILD_TYPE Debug)
4955
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
5258
set(CMAKE_CXX_FLAGS_RELEASE
5359
"-Os -ffunction-sections -fdata-sections -s -fno-exceptions -fno-rtti")
5460
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g")
@@ -60,8 +66,7 @@ set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g")
6066
option(
6167
REGISTER_EXAMPLE_CUSTOM_OP
6268
"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)
6570

6671
# Option to register quantized ops with quantized kernels. See
6772
# kernels/quantized/CMakeLists.txt
@@ -86,12 +91,6 @@ endif()
8691
option(EXECUTORCH_BUILD_XNNPACK
8792
"Build xnn_executor_runner which depends on XNNPACK" OFF)
8893

89-
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
90-
91-
if(NOT CMAKE_CXX_STANDARD)
92-
set(CMAKE_CXX_STANDARD 17)
93-
endif()
94-
9594
if(NOT BUCK2)
9695
set(BUCK2 buck2)
9796
endif()
@@ -125,11 +124,68 @@ endif()
125124
message(STATUS "executorch: Using sources file ${EXECUTORCH_SRCS_FILE}")
126125
include(${EXECUTORCH_SRCS_FILE})
127126

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+
128169
#
129170
# flatc: Flatbuffer commandline tool to generate .h files from .fbs files
130171
#
131172

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()
133189
set(FLATC_EXECUTABLE flatc)
134190
option(FLATBUFFERS_BUILD_FLATC "" ON)
135191
option(FLATBUFFERS_BUILD_FLATHASH "" OFF)
@@ -138,12 +194,13 @@ if(NOT FLATC_EXECUTABLE)
138194
option(FLATBUFFERS_INSTALL "" OFF)
139195
add_subdirectory(third-party/flatbuffers)
140196
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()
147204

148205
#
149206
# program_schema: Generated .h files from schema/*.fbs inputs
@@ -175,27 +232,42 @@ endif()
175232
# operators necessary for the models that will run.
176233
#
177234
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/kernels/portable)
178-
set(_libs executorch portable_ops_lib gflags)
179235

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)
184243
endif()
185244

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()
191265

192-
# ios can only build library but not binary
193-
if(NOT CMAKE_TOOLCHAIN_FILE MATCHES ".*ios\.toolchain\.cmake$")
194266
add_executable(executor_runner ${_executor_runner__srcs})
195267
if(CMAKE_BUILD_TYPE EQUAL "RELEASE")
196268
target_link_options(executor_runner PRIVATE "LINKER:--gc-sections")
197269
endif()
198-
target_link_libraries(executor_runner ${_libs})
270+
target_link_libraries(executor_runner ${_executor_runner_libs})
199271
target_compile_options(executor_runner PUBLIC ${_common_compile_options})
200272
endif()
201273

backends/xnnpack/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ target_include_directories(
6262
xnnpack_schema INTERFACE ${_xnnpack_schema__include_dir}
6363
${EXECUTORCH_ROOT}/third-party/flatbuffers/include)
6464

65-
set(xnn_executor_runner_libs ${_libs} xnnpack_schema)
65+
set(xnn_executor_runner_libs ${_executor_runner_libs} xnnpack_schema)
6666

6767
list(TRANSFORM _dynamic_quant_utils__srcs PREPEND "${EXECUTORCH_ROOT}/")
6868
add_library(dynamic_quant_utils ${_dynamic_quant_utils__srcs})

build/Utils.cmake

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,40 @@
44
# This source code is licensed under the BSD-style license found in the
55
# LICENSE file in the root directory of this source tree.
66

7+
#
78
# This file is intended to have helper functions to keep the CMakeLists.txt
89
# concise. If there are any helper function can be re-used, it's recommented to
910
# add them here.
11+
#
12+
# ### Editing this file ###
13+
#
14+
# This file should be formatted with
15+
# ~~~
16+
# cmake-format --first-comment-is-literal=True Utils.cmake
17+
# ~~~
18+
# It should also be cmake-lint clean.
19+
#
1020

11-
# Public function to print summary for all configurations. For new variable,
21+
# Public function to print summary for all configurations. For new variables,
1222
# it's recommended to add them here.
1323
function(executorch_print_configuration_summary)
1424
message(STATUS "")
1525
message(STATUS "******** Summary ********")
16-
message(STATUS " BUCK : ${BUCK2}")
1726
message(STATUS " CMAKE_BUILD_TYPE : ${CMAKE_BUILD_TYPE}")
1827
message(STATUS " CMAKE_CXX_STANDARD : ${CMAKE_CXX_STANDARD}")
1928
message(STATUS " CMAKE_CXX_COMPILER_ID : ${CMAKE_CXX_COMPILER_ID}")
2029
message(STATUS " CMAKE_TOOLCHAIN_FILE : ${CMAKE_TOOLCHAIN_FILE}")
30+
message(STATUS " BUCK2 : ${BUCK2}")
2131
message(STATUS " PYTHON_EXECUTABLE : ${PYTHON_EXECUTABLE}")
2232
message(STATUS " FLATC_EXECUTABLE : ${FLATC_EXECUTABLE}")
23-
message(STATUS " FLATBUFFERS_BUILD_FLATC : ${FLATBUFFERS_BUILD_FLATC}")
2433
message(
25-
STATUS " FLATBUFFERS_BUILD_FLATHASH : ${FLATBUFFERS_BUILD_FLATHASH}")
34+
STATUS " EXECUTORCH_BUILD_HOST_TARGETS : ${EXECUTORCH_BUILD_HOST_TARGETS}")
35+
message(STATUS " EXECUTORCH_BUILD_FLATC : ${EXECUTORCH_BUILD_FLATC}")
36+
message(STATUS " EXECUTORCH_BUILD_GFLAGS : ${EXECUTORCH_BUILD_GFLAGS}")
2637
message(
27-
STATUS " FLATBUFFERS_BUILD_FLATLIB : ${FLATBUFFERS_BUILD_FLATLIB}")
28-
message(STATUS " FLATBUFFERS_BUILD_TESTS : ${FLATBUFFERS_BUILD_TESTS}")
38+
STATUS
39+
" EXECUTORCH_BUILD_EXECUTOR_RUNNER : ${EXECUTORCH_BUILD_EXECUTOR_RUNNER}"
40+
)
2941
message(
3042
STATUS " REGISTER_EXAMPLE_CUSTOM_OPS : ${REGISTER_EXAMPLE_CUSTOM_OPS}")
3143
endfunction()
@@ -37,24 +49,20 @@ function(kernel_link_options target_name)
3749
# target_link_options(${target_name} INTERFACE
3850
# "$<LINK_LIBRARY:WHOLE_ARCHIVE,target_name>")
3951
target_link_options(
40-
${target_name}
41-
INTERFACE
42-
"SHELL:LINKER:--whole-archive \
52+
${target_name} INTERFACE "SHELL:LINKER:--whole-archive \
4353
$<TARGET_FILE:${target_name}> \
44-
LINKER:--no-whole-archive"
45-
)
54+
LINKER:--no-whole-archive")
4655
endfunction()
4756

57+
# Same as kernel_link_options but it's for MacOS linker
4858
function(macos_kernel_link_options target_name)
49-
target_link_options(
50-
${target_name} INTERFACE
51-
# Same as kernel_link_options but it's for MacOS linker
52-
"SHELL:LINKER:-force_load,$<TARGET_FILE:${target_name}>")
59+
target_link_options(${target_name} INTERFACE
60+
"SHELL:LINKER:-force_load,$<TARGET_FILE:${target_name}>")
5361
endfunction()
5462

63+
# Ensure that the load-time constructor functions run. By default, the linker
64+
# would remove them since there are no other references to them.
5565
function(target_link_options_shared_lib target_name)
56-
# Ensure that the load-time constructor functions run. By default, the linker
57-
# would remove them since there are no other references to them.
5866
if(APPLE)
5967
macos_kernel_link_options(${target_name})
6068
else()
@@ -65,13 +73,15 @@ endfunction()
6573
# Extract source files based on toml config. This is useful to keep buck2 and
6674
# cmake aligned.
6775
function(extract_sources sources_file)
68-
if(NOT EXECUTORCH_ROOT)
69-
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR})
76+
if(EXECUTORCH_ROOT)
77+
set(executorch_root ${EXECUTORCH_ROOT})
78+
else()
79+
set(executorch_root ${CMAKE_CURRENT_SOURCE_DIR})
7080
endif()
7181
execute_process(
7282
COMMAND
73-
${PYTHON_EXECUTABLE} ${EXECUTORCH_ROOT}/build/extract_sources.py
74-
--buck2=${BUCK2} --config=${EXECUTORCH_ROOT}/build/cmake_deps.toml
83+
${PYTHON_EXECUTABLE} ${executorch_root}/build/extract_sources.py
84+
--buck2=${BUCK2} --config=${executorch_root}/build/cmake_deps.toml
7585
--out=${sources_file}
7686
OUTPUT_VARIABLE gen_srcs_output
7787
ERROR_VARIABLE gen_srcs_error

0 commit comments

Comments
 (0)