Skip to content

Build flatc for the host instead of the target platform #9077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 49 additions & 30 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,17 @@ cmake_dependent_option(
"NOT FLATC_EXECUTABLE;EXECUTORCH_BUILD_HOST_TARGETS" OFF
)


set(FLATBUFFERS_BUILD_FLATC OFF CACHE BOOL "")
set(FLATBUFFERS_BUILD_FLATHASH OFF CACHE BOOL "")
set(FLATBUFFERS_BUILD_FLATLIB OFF CACHE BOOL "")
set(FLATBUFFERS_BUILD_TESTS OFF CACHE BOOL "")
set(FLATBUFFERS_INSTALL OFF CACHE BOOL "")
# exir lets users set the alignment of tensor data embedded in the flatbuffer,
# and some users need an alignment larger than the default, which is typically
# 32.
set(FLATBUFFERS_MAX_ALIGNMENT 1024)

if(EXECUTORCH_BUILD_FLATC)
if(FLATC_EXECUTABLE)
# We could ignore this, but it could lead to confusion about which `flatc`
Expand All @@ -482,41 +493,49 @@ if(EXECUTORCH_BUILD_FLATC)
FATAL_ERROR "May not set both EXECUTORCH_BUILD_FLATC and FLATC_EXECUTABLE"
)
endif()
set(FLATC_EXECUTABLE flatc)
set(FLATBUFFERS_BUILD_FLATC
ON
CACHE BOOL ""
)
set(FLATBUFFERS_BUILD_FLATHASH
OFF
CACHE BOOL ""
)
set(FLATBUFFERS_BUILD_FLATLIB
OFF
CACHE BOOL ""
)
set(FLATBUFFERS_BUILD_TESTS
OFF
CACHE BOOL ""
)
set(FLATBUFFERS_INSTALL
OFF
CACHE BOOL ""
)
add_subdirectory(third-party/flatbuffers)

# exir lets users set the alignment of tensor data embedded in the flatbuffer,
# and some users need an alignment larger than the default, which is typically
# 32.
target_compile_definitions(flatc PRIVATE FLATBUFFERS_MAX_ALIGNMENT=1024)
# Build flatc for the *host* to generate files as part of the build step.
include(ExternalProject)
ExternalProject_Add(
flatbuffers
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/third-party/flatbuffers
BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/third-party/flatbuffers
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third-party/flatbuffers
CMAKE_ARGS -DFLATBUFFERS_BUILD_FLATC=ON
-DFLATBUFFERS_BUILD_FLATHASH=${FLATBUFFERS_BUILD_FLATHASH}
-DFLATBUFFERS_BUILD_FLATLIB=${FLATBUFFERS_BUILD_FLATLIB}
-DFLATBUFFERS_BUILD_TESTS=${FLATBUFFERS_BUILD_TESTS}
-DFLATBUFFERS_INSTALL=${FLATBUFFERS_INSTALL}
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_CXX_FLAGS="-DFLATBUFFERS_MAX_ALIGNMENT=${FLATBUFFERS_MAX_ALIGNMENT}"
INSTALL_COMMAND ""
)
ExternalProject_Get_Property(flatbuffers BINARY_DIR)
set(FLATC_EXECUTABLE ${BINARY_DIR}/flatc)
set(FLATC_EXECUTABLE_BUILT_FROM_SOURCE YES)
endif()

if(NOT FLATC_EXECUTABLE)
message(
FATAL_ERROR
"FLATC_EXECUTABLE must be set when EXECUTORCH_BUILD_FLATC is disabled. "
"Note that EXECUTORCH_BUILD_FLATC may be disabled implicitly when "
"cross-compiling or when EXECUTORCH_BUILD_HOST_TARGETS is disabled."
WARNING "FLATC_EXECUTABLE not specified, looking for flatc"
)
find_program(FLATC_EXECUTABLE flatc)

if(NOT FLATC_EXECUTABLE)
message(
FATAL_ERROR
"FLATC_EXECUTABLE must be set when EXECUTORCH_BUILD_FLATC is disabled. "
"Note that EXECUTORCH_BUILD_FLATC may be disabled implicitly when "
"cross-compiling or when EXECUTORCH_BUILD_HOST_TARGETS is disabled."
)
endif()
endif()

add_executable(flatc IMPORTED GLOBAL)
set_target_properties(flatc PROPERTIES IMPORTED_LOCATION ${FLATC_EXECUTABLE})

if(FLATC_EXECUTABLE_BUILT_FROM_SOURCE)
add_dependencies(flatc flatbuffers)
endif()

#
Expand Down
5 changes: 1 addition & 4 deletions backends/apple/mps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ if(NOT PYTHON_EXECUTABLE)
resolve_python_executable()
endif()

if(NOT FLATC_EXECUTABLE)
set(FLATC_EXECUTABLE flatc)
endif()

set(_common_compile_options -Wno-deprecated-declarations)
set(_common_include_directories ${EXECUTORCH_ROOT}/..)

Expand All @@ -50,6 +46,7 @@ add_custom_command(
"${_mps_schema__include_dir}/executorch/backends/apple/mps"
${_mps_schema__srcs}
WORKING_DIRECTORY ${EXECUTORCH_ROOT}
DEPENDS flatc
COMMENT "Generating mps_schema headers"
VERBATIM
)
Expand Down
6 changes: 2 additions & 4 deletions backends/qualcomm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,13 @@ if(${ANDROID})
find_library(android_log log)
endif()

if(NOT FLATC_EXECUTABLE)
set(FLATC_EXECUTABLE flatc)
endif()

set(qcir_schema_include_dir ${CMAKE_CURRENT_LIST_DIR}/aot/ir)
set(qcir_schema_output ${qcir_schema_include_dir}/qcir_generated.h)
add_custom_command(
OUTPUT qcir_schema_output
COMMAND ${FLATC_EXECUTABLE} --cpp --cpp-std c++11 --scoped-enums -o
${qcir_schema_include_dir} ${qcir_schema_include_dir}/qcir.fbs
DEPENDS flatc
COMMENT "Generating qualcomm ir schema headers"
VERBATIM
)
Expand Down Expand Up @@ -100,6 +97,7 @@ add_custom_command(
"${_qnn_schema__include_dir}/executorch/backends/qualcomm"
${_qnn_schema__srcs}
WORKING_DIRECTORY ${EXECUTORCH_SOURCE_DIR}
DEPENDS flatc
COMMENT "Generating qnn_schema headers"
VERBATIM
)
Expand Down
5 changes: 1 addition & 4 deletions backends/vulkan/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ if(NOT PYTHON_EXECUTABLE)
set(PYTHON_EXECUTABLE python3)
endif()

if(NOT FLATC_EXECUTABLE)
set(FLATC_EXECUTABLE flatc)
endif()

# Include this file to access target_link_options_shared_lib This is required to
# provide access to target_link_options_shared_lib which allows libraries to be
# linked with the --whole-archive flag. This is required for libraries that
Expand Down Expand Up @@ -92,6 +88,7 @@ add_custom_command(
${FLATC_EXECUTABLE} --cpp --cpp-std c++11 --scoped-enums -o
"${SCHEMA_INCLUDE_DIR}/executorch/backends/vulkan/serialization/" ${_vulkan_schema__srcs}
WORKING_DIRECTORY ${EXECUTORCH_ROOT}
DEPENDS flatc
COMMENT "Generating vulkan_schema headers"
VERBATIM
)
Expand Down
5 changes: 1 addition & 4 deletions backends/xnnpack/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
endif()

if(NOT FLATC_EXECUTABLE)
set(FLATC_EXECUTABLE flatc)
endif()

# Source root directory for executorch.
if(NOT EXECUTORCH_ROOT)
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
Expand Down Expand Up @@ -82,6 +78,7 @@ add_custom_command(
${_xnnpack_schema__srcs}
COMMAND mv ${_xnnpack_flatbuffer__outputs} ${_xnnpack_schema__outputs}
WORKING_DIRECTORY ${EXECUTORCH_ROOT}
DEPENDS flatc
COMMENT "Generating xnnpack_schema headers"
VERBATIM
)
Expand Down
6 changes: 1 addition & 5 deletions devtools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ if(NOT PYTHON_EXECUTABLE)
resolve_python_executable()
endif()

if(NOT FLATC_EXECUTABLE)
set(FLATC_EXECUTABLE flatc)
endif()

# Paths to headers generated from the .fbs files. set(_etdump_schemas
# etdump_schema_flatcc.fbs scalar_type.fbs)

Expand Down Expand Up @@ -205,7 +201,7 @@ add_custom_command(
"${_bundled_schema__include_dir}/executorch/devtools/bundled_program/schema"
${_bundled_program_schema__srcs}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/devtools
DEPENDS ${FLATC_EXECUTABLE} ${_bundled_program_schema__srcs}
DEPENDS flatc ${_bundled_program_schema__srcs}
COMMENT "Generating bundled_program headers"
VERBATIM
)
Expand Down
4 changes: 0 additions & 4 deletions examples/apple/mps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 17)
endif()

if(NOT FLATC_EXECUTABLE)
set(FLATC_EXECUTABLE flatc)
endif()

# Source root directory for executorch.
if(NOT EXECUTORCH_ROOT)
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
Expand Down
8 changes: 2 additions & 6 deletions extension/flat_tensor/serialize/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
# cmake-format -i CMakeLists.txt
# ~~~

if(NOT FLATC_EXECUTABLE)
set(FLATC_EXECUTABLE flatc)
endif()

# The include directory that will contain the generated schema headers.
set(_flat_tensor_schema__include_dir "${CMAKE_BINARY_DIR}/extension/flat_tensor/include")
set(_flat_tensor_schema__output_dir "${_flat_tensor_schema__include_dir}/executorch/extension/flat_tensor/serialize")
Expand All @@ -37,7 +33,7 @@ function(generate_flat_tensor_schema _schema_srcs _schema_name)
${FLATC_EXECUTABLE} --cpp --cpp-std c++11 --gen-mutable --scoped-enums -o
"${_flat_tensor_schema__output_dir}" ${_schema_srcs}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS ${FLATC_EXECUTABLE} ${_schema_srcs}
DEPENDS flatc ${_schema_srcs}
COMMENT "Generating ${_schema_name} headers"
VERBATIM
)
Expand All @@ -49,7 +45,7 @@ function(generate_flat_tensor_schema _schema_srcs _schema_name)
# and some users need an alignment larger than the default, which is typically
# 32.
target_compile_definitions(
${_schema_name} INTERFACE FLATBUFFERS_MAX_ALIGNMENT=1024
${_schema_name} INTERFACE FLATBUFFERS_MAX_ALIGNMENT=${FLATBUFFERS_MAX_ALIGNMENT}
)

target_include_directories(
Expand Down
8 changes: 2 additions & 6 deletions schema/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
# cmake-format -i CMakeLists.txt
# ~~~

if(NOT FLATC_EXECUTABLE)
set(FLATC_EXECUTABLE flatc)
endif()

# The include directory that will contain the generated schema headers.
set(_program_schema__include_dir "${CMAKE_BINARY_DIR}/schema/include")
set(_program_schema__output_dir "${_program_schema__include_dir}/executorch/schema")
Expand All @@ -37,7 +33,7 @@ function(generate_program_schema _schema_srcs _schema_name)
${FLATC_EXECUTABLE} --cpp --cpp-std c++11 --gen-mutable --scoped-enums -o
"${_program_schema__output_dir}" ${_schema_srcs}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS ${FLATC_EXECUTABLE} ${_schema_srcs}
DEPENDS flatc ${_schema_srcs}
COMMENT "Generating ${_schema_name} headers"
VERBATIM
)
Expand All @@ -49,7 +45,7 @@ function(generate_program_schema _schema_srcs _schema_name)
# and some users need an alignment larger than the default, which is typically
# 32.
target_compile_definitions(
${_schema_name} INTERFACE FLATBUFFERS_MAX_ALIGNMENT=1024
${_schema_name} INTERFACE FLATBUFFERS_MAX_ALIGNMENT=${FLATBUFFERS_MAX_ALIGNMENT}
)

target_include_directories(
Expand Down
4 changes: 0 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,10 +652,6 @@ def run(self):

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

# TODO(dbort): Try to manage these targets and the cmake args from the
# extension entries themselves instead of hard-coding them here.
build_args += ["--target", "flatc"]

if ShouldBuild.pybindings():
cmake_args += [
"-DEXECUTORCH_BUILD_PYBIND=ON",
Expand Down
Loading