Skip to content

Commit 570e06c

Browse files
authored
Build flatc for the host instead of the target platform (#9077)
### Summary * Fixes #7260 * We use flatc as part of the build step to generate some files from FlatBuffer definitions. This implies the tool runs on the host. However, we currently build flatc as part of the main build — which propagates the target CXX flags to flatc. Thus, flatc gets built targeting the target platform. To ensure flatc is built for the host, we can use include flatc as an [ExternalProject](https://cmake.org/cmake/help/latest/module/ExternalProject.html). This does not propagate the CXX flags * Some targets implicitly depended on flatc, we now make that requirement explicit * We currently spread the `FLATC_EXECUTABLE` defaulting across the project. Let's just centralize this at the root ### Test plan ```bash $ ./install_executorch.sh # Previously flatc was built against the Android target, now they target the host $ ./build/build_android_library.sh $ file /Users/jathu/executorch/cmake-out-android-arm64-v8a/third-party/flatbuffers/flatc /Users/jathu/executorch/cmake-out-android-arm64-v8a/third-party/flatbuffers/flatc: Mach-O 64-bit executable arm64 # Apple builds work as usual, but they use flatc from pip $ ./build/build_apple_frameworks.sh ``` cc @larryliu0820 @lucylq @swolchok @dbort
1 parent 08f0f7a commit 570e06c

File tree

10 files changed

+59
-71
lines changed

10 files changed

+59
-71
lines changed

CMakeLists.txt

Lines changed: 49 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,17 @@ cmake_dependent_option(
474474
"NOT FLATC_EXECUTABLE;EXECUTORCH_BUILD_HOST_TARGETS" OFF
475475
)
476476

477+
478+
set(FLATBUFFERS_BUILD_FLATC OFF CACHE BOOL "")
479+
set(FLATBUFFERS_BUILD_FLATHASH OFF CACHE BOOL "")
480+
set(FLATBUFFERS_BUILD_FLATLIB OFF CACHE BOOL "")
481+
set(FLATBUFFERS_BUILD_TESTS OFF CACHE BOOL "")
482+
set(FLATBUFFERS_INSTALL OFF CACHE BOOL "")
483+
# exir lets users set the alignment of tensor data embedded in the flatbuffer,
484+
# and some users need an alignment larger than the default, which is typically
485+
# 32.
486+
set(FLATBUFFERS_MAX_ALIGNMENT 1024)
487+
477488
if(EXECUTORCH_BUILD_FLATC)
478489
if(FLATC_EXECUTABLE)
479490
# We could ignore this, but it could lead to confusion about which `flatc`
@@ -482,41 +493,49 @@ if(EXECUTORCH_BUILD_FLATC)
482493
FATAL_ERROR "May not set both EXECUTORCH_BUILD_FLATC and FLATC_EXECUTABLE"
483494
)
484495
endif()
485-
set(FLATC_EXECUTABLE flatc)
486-
set(FLATBUFFERS_BUILD_FLATC
487-
ON
488-
CACHE BOOL ""
489-
)
490-
set(FLATBUFFERS_BUILD_FLATHASH
491-
OFF
492-
CACHE BOOL ""
493-
)
494-
set(FLATBUFFERS_BUILD_FLATLIB
495-
OFF
496-
CACHE BOOL ""
497-
)
498-
set(FLATBUFFERS_BUILD_TESTS
499-
OFF
500-
CACHE BOOL ""
501-
)
502-
set(FLATBUFFERS_INSTALL
503-
OFF
504-
CACHE BOOL ""
505-
)
506-
add_subdirectory(third-party/flatbuffers)
507496

508-
# exir lets users set the alignment of tensor data embedded in the flatbuffer,
509-
# and some users need an alignment larger than the default, which is typically
510-
# 32.
511-
target_compile_definitions(flatc PRIVATE FLATBUFFERS_MAX_ALIGNMENT=1024)
497+
# Build flatc for the *host* to generate files as part of the build step.
498+
include(ExternalProject)
499+
ExternalProject_Add(
500+
flatbuffers
501+
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/third-party/flatbuffers
502+
BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/third-party/flatbuffers
503+
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third-party/flatbuffers
504+
CMAKE_ARGS -DFLATBUFFERS_BUILD_FLATC=ON
505+
-DFLATBUFFERS_BUILD_FLATHASH=${FLATBUFFERS_BUILD_FLATHASH}
506+
-DFLATBUFFERS_BUILD_FLATLIB=${FLATBUFFERS_BUILD_FLATLIB}
507+
-DFLATBUFFERS_BUILD_TESTS=${FLATBUFFERS_BUILD_TESTS}
508+
-DFLATBUFFERS_INSTALL=${FLATBUFFERS_INSTALL}
509+
-DCMAKE_BUILD_TYPE=Release
510+
-DCMAKE_CXX_FLAGS="-DFLATBUFFERS_MAX_ALIGNMENT=${FLATBUFFERS_MAX_ALIGNMENT}"
511+
INSTALL_COMMAND ""
512+
)
513+
ExternalProject_Get_Property(flatbuffers BINARY_DIR)
514+
set(FLATC_EXECUTABLE ${BINARY_DIR}/flatc)
515+
set(FLATC_EXECUTABLE_BUILT_FROM_SOURCE YES)
512516
endif()
517+
513518
if(NOT FLATC_EXECUTABLE)
514519
message(
515-
FATAL_ERROR
516-
"FLATC_EXECUTABLE must be set when EXECUTORCH_BUILD_FLATC is disabled. "
517-
"Note that EXECUTORCH_BUILD_FLATC may be disabled implicitly when "
518-
"cross-compiling or when EXECUTORCH_BUILD_HOST_TARGETS is disabled."
520+
WARNING "FLATC_EXECUTABLE not specified, looking for flatc"
519521
)
522+
find_program(FLATC_EXECUTABLE flatc)
523+
524+
if(NOT FLATC_EXECUTABLE)
525+
message(
526+
FATAL_ERROR
527+
"FLATC_EXECUTABLE must be set when EXECUTORCH_BUILD_FLATC is disabled. "
528+
"Note that EXECUTORCH_BUILD_FLATC may be disabled implicitly when "
529+
"cross-compiling or when EXECUTORCH_BUILD_HOST_TARGETS is disabled."
530+
)
531+
endif()
532+
endif()
533+
534+
add_executable(flatc IMPORTED GLOBAL)
535+
set_target_properties(flatc PROPERTIES IMPORTED_LOCATION ${FLATC_EXECUTABLE})
536+
537+
if(FLATC_EXECUTABLE_BUILT_FROM_SOURCE)
538+
add_dependencies(flatc flatbuffers)
520539
endif()
521540

522541
#

backends/apple/mps/CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ if(NOT PYTHON_EXECUTABLE)
2222
resolve_python_executable()
2323
endif()
2424

25-
if(NOT FLATC_EXECUTABLE)
26-
set(FLATC_EXECUTABLE flatc)
27-
endif()
28-
2925
set(_common_compile_options -Wno-deprecated-declarations)
3026
set(_common_include_directories ${EXECUTORCH_ROOT}/..)
3127

@@ -50,6 +46,7 @@ add_custom_command(
5046
"${_mps_schema__include_dir}/executorch/backends/apple/mps"
5147
${_mps_schema__srcs}
5248
WORKING_DIRECTORY ${EXECUTORCH_ROOT}
49+
DEPENDS flatc
5350
COMMENT "Generating mps_schema headers"
5451
VERBATIM
5552
)

backends/qualcomm/CMakeLists.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,13 @@ if(${ANDROID})
3939
find_library(android_log log)
4040
endif()
4141

42-
if(NOT FLATC_EXECUTABLE)
43-
set(FLATC_EXECUTABLE flatc)
44-
endif()
45-
4642
set(qcir_schema_include_dir ${CMAKE_CURRENT_LIST_DIR}/aot/ir)
4743
set(qcir_schema_output ${qcir_schema_include_dir}/qcir_generated.h)
4844
add_custom_command(
4945
OUTPUT qcir_schema_output
5046
COMMAND ${FLATC_EXECUTABLE} --cpp --cpp-std c++11 --scoped-enums -o
5147
${qcir_schema_include_dir} ${qcir_schema_include_dir}/qcir.fbs
48+
DEPENDS flatc
5249
COMMENT "Generating qualcomm ir schema headers"
5350
VERBATIM
5451
)
@@ -100,6 +97,7 @@ add_custom_command(
10097
"${_qnn_schema__include_dir}/executorch/backends/qualcomm"
10198
${_qnn_schema__srcs}
10299
WORKING_DIRECTORY ${EXECUTORCH_SOURCE_DIR}
100+
DEPENDS flatc
103101
COMMENT "Generating qnn_schema headers"
104102
VERBATIM
105103
)

backends/vulkan/CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ if(NOT PYTHON_EXECUTABLE)
2828
set(PYTHON_EXECUTABLE python3)
2929
endif()
3030

31-
if(NOT FLATC_EXECUTABLE)
32-
set(FLATC_EXECUTABLE flatc)
33-
endif()
34-
3531
# Include this file to access target_link_options_shared_lib This is required to
3632
# provide access to target_link_options_shared_lib which allows libraries to be
3733
# linked with the --whole-archive flag. This is required for libraries that
@@ -92,6 +88,7 @@ add_custom_command(
9288
${FLATC_EXECUTABLE} --cpp --cpp-std c++11 --scoped-enums -o
9389
"${SCHEMA_INCLUDE_DIR}/executorch/backends/vulkan/serialization/" ${_vulkan_schema__srcs}
9490
WORKING_DIRECTORY ${EXECUTORCH_ROOT}
91+
DEPENDS flatc
9592
COMMENT "Generating vulkan_schema headers"
9693
VERBATIM
9794
)

backends/xnnpack/CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ if(NOT CMAKE_CXX_STANDARD)
1818
set(CMAKE_CXX_STANDARD 17)
1919
endif()
2020

21-
if(NOT FLATC_EXECUTABLE)
22-
set(FLATC_EXECUTABLE flatc)
23-
endif()
24-
2521
# Source root directory for executorch.
2622
if(NOT EXECUTORCH_ROOT)
2723
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
@@ -82,6 +78,7 @@ add_custom_command(
8278
${_xnnpack_schema__srcs}
8379
COMMAND mv ${_xnnpack_flatbuffer__outputs} ${_xnnpack_schema__outputs}
8480
WORKING_DIRECTORY ${EXECUTORCH_ROOT}
81+
DEPENDS flatc
8582
COMMENT "Generating xnnpack_schema headers"
8683
VERBATIM
8784
)

devtools/CMakeLists.txt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ if(NOT PYTHON_EXECUTABLE)
3838
resolve_python_executable()
3939
endif()
4040

41-
if(NOT FLATC_EXECUTABLE)
42-
set(FLATC_EXECUTABLE flatc)
43-
endif()
44-
4541
# Paths to headers generated from the .fbs files. set(_etdump_schemas
4642
# etdump_schema_flatcc.fbs scalar_type.fbs)
4743

@@ -205,7 +201,7 @@ add_custom_command(
205201
"${_bundled_schema__include_dir}/executorch/devtools/bundled_program/schema"
206202
${_bundled_program_schema__srcs}
207203
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/devtools
208-
DEPENDS ${FLATC_EXECUTABLE} ${_bundled_program_schema__srcs}
204+
DEPENDS flatc ${_bundled_program_schema__srcs}
209205
COMMENT "Generating bundled_program headers"
210206
VERBATIM
211207
)

examples/apple/mps/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ if(NOT CMAKE_CXX_STANDARD)
1818
set(CMAKE_CXX_STANDARD 17)
1919
endif()
2020

21-
if(NOT FLATC_EXECUTABLE)
22-
set(FLATC_EXECUTABLE flatc)
23-
endif()
24-
2521
# Source root directory for executorch.
2622
if(NOT EXECUTORCH_ROOT)
2723
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)

extension/flat_tensor/serialize/CMakeLists.txt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
# cmake-format -i CMakeLists.txt
1010
# ~~~
1111

12-
if(NOT FLATC_EXECUTABLE)
13-
set(FLATC_EXECUTABLE flatc)
14-
endif()
15-
1612
# The include directory that will contain the generated schema headers.
1713
set(_flat_tensor_schema__include_dir "${CMAKE_BINARY_DIR}/extension/flat_tensor/include")
1814
set(_flat_tensor_schema__output_dir "${_flat_tensor_schema__include_dir}/executorch/extension/flat_tensor/serialize")
@@ -37,7 +33,7 @@ function(generate_flat_tensor_schema _schema_srcs _schema_name)
3733
${FLATC_EXECUTABLE} --cpp --cpp-std c++11 --gen-mutable --scoped-enums -o
3834
"${_flat_tensor_schema__output_dir}" ${_schema_srcs}
3935
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
40-
DEPENDS ${FLATC_EXECUTABLE} ${_schema_srcs}
36+
DEPENDS flatc ${_schema_srcs}
4137
COMMENT "Generating ${_schema_name} headers"
4238
VERBATIM
4339
)
@@ -49,7 +45,7 @@ function(generate_flat_tensor_schema _schema_srcs _schema_name)
4945
# and some users need an alignment larger than the default, which is typically
5046
# 32.
5147
target_compile_definitions(
52-
${_schema_name} INTERFACE FLATBUFFERS_MAX_ALIGNMENT=1024
48+
${_schema_name} INTERFACE FLATBUFFERS_MAX_ALIGNMENT=${FLATBUFFERS_MAX_ALIGNMENT}
5349
)
5450

5551
target_include_directories(

schema/CMakeLists.txt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
# cmake-format -i CMakeLists.txt
1010
# ~~~
1111

12-
if(NOT FLATC_EXECUTABLE)
13-
set(FLATC_EXECUTABLE flatc)
14-
endif()
15-
1612
# The include directory that will contain the generated schema headers.
1713
set(_program_schema__include_dir "${CMAKE_BINARY_DIR}/schema/include")
1814
set(_program_schema__output_dir "${_program_schema__include_dir}/executorch/schema")
@@ -37,7 +33,7 @@ function(generate_program_schema _schema_srcs _schema_name)
3733
${FLATC_EXECUTABLE} --cpp --cpp-std c++11 --gen-mutable --scoped-enums -o
3834
"${_program_schema__output_dir}" ${_schema_srcs}
3935
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
40-
DEPENDS ${FLATC_EXECUTABLE} ${_schema_srcs}
36+
DEPENDS flatc ${_schema_srcs}
4137
COMMENT "Generating ${_schema_name} headers"
4238
VERBATIM
4339
)
@@ -49,7 +45,7 @@ function(generate_program_schema _schema_srcs _schema_name)
4945
# and some users need an alignment larger than the default, which is typically
5046
# 32.
5147
target_compile_definitions(
52-
${_schema_name} INTERFACE FLATBUFFERS_MAX_ALIGNMENT=1024
48+
${_schema_name} INTERFACE FLATBUFFERS_MAX_ALIGNMENT=${FLATBUFFERS_MAX_ALIGNMENT}
5349
)
5450

5551
target_include_directories(

setup.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -652,10 +652,6 @@ def run(self):
652652

653653
build_args = [f"-j{self.parallel}"]
654654

655-
# TODO(dbort): Try to manage these targets and the cmake args from the
656-
# extension entries themselves instead of hard-coding them here.
657-
build_args += ["--target", "flatc"]
658-
659655
if ShouldBuild.pybindings():
660656
cmake_args += [
661657
"-DEXECUTORCH_BUILD_PYBIND=ON",

0 commit comments

Comments
 (0)