Skip to content

Commit 1c2b7ba

Browse files
authored
Remove FLATC_EXECUTABLE and the ability to bring your own flatc (#10781)
### Summary * Remove the ability to set flatc using FLATC_EXECUTABLE and always build it from source * Also remove, the now redundant, `EXECUTORCH_BUILD_FLATC` * Move the flatc configuration into `third-party/CMakeLists.txt` to keep things organized * Allow `FLATBUFFERS_MAX_ALIGNMENT` to be configurable ### Test plan CI cc @larryliu0820
1 parent bf50527 commit 1c2b7ba

File tree

12 files changed

+82
-112
lines changed

12 files changed

+82
-112
lines changed

CMakeLists.txt

Lines changed: 3 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ print_configured_options()
5959

6060
include(tools/cmake/Utils.cmake)
6161
include(CMakeDependentOption)
62+
include(ExternalProject)
6263

6364
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
6465

@@ -260,6 +261,8 @@ cmake_dependent_option(
260261
"NOT EXECUTORCH_BUILD_ARM_BAREMETAL" OFF
261262
)
262263

264+
add_subdirectory(third-party)
265+
263266
if(EXECUTORCH_BUILD_EXTENSION_TRAINING)
264267
set(EXECUTORCH_BUILD_EXTENSION_DATA_LOADER ON)
265268
set(EXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR ON)
@@ -454,81 +457,6 @@ if(EXECUTORCH_USE_CPP_CODE_COVERAGE)
454457
endif()
455458
endif()
456459

457-
#
458-
# flatc: Flatbuffer commandline tool to generate .h files from .fbs files
459-
#
460-
cmake_dependent_option(
461-
EXECUTORCH_BUILD_FLATC "Build the flatc executable." ON
462-
"NOT FLATC_EXECUTABLE" OFF
463-
)
464-
465-
set(FLATBUFFERS_BUILD_FLATC OFF CACHE BOOL "")
466-
set(FLATBUFFERS_BUILD_FLATHASH OFF CACHE BOOL "")
467-
set(FLATBUFFERS_BUILD_FLATLIB OFF CACHE BOOL "")
468-
set(FLATBUFFERS_BUILD_TESTS OFF CACHE BOOL "")
469-
set(FLATBUFFERS_INSTALL OFF CACHE BOOL "")
470-
# exir lets users set the alignment of tensor data embedded in the flatbuffer,
471-
# and some users need an alignment larger than the default, which is typically
472-
# 32.
473-
set(FLATBUFFERS_MAX_ALIGNMENT 1024)
474-
475-
if(EXECUTORCH_BUILD_FLATC)
476-
if(FLATC_EXECUTABLE)
477-
# We could ignore this, but it could lead to confusion about which `flatc`
478-
# is actually being used.
479-
message(
480-
FATAL_ERROR "May not set both EXECUTORCH_BUILD_FLATC and FLATC_EXECUTABLE"
481-
)
482-
endif()
483-
484-
# Build flatc for the *host* to generate files as part of the build step.
485-
include(ExternalProject)
486-
ExternalProject_Add(
487-
flatbuffers
488-
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/third-party/flatbuffers
489-
BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/third-party/flatbuffers
490-
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third-party/flatbuffers
491-
CMAKE_ARGS -DFLATBUFFERS_BUILD_FLATC=ON
492-
-DFLATBUFFERS_BUILD_FLATHASH=${FLATBUFFERS_BUILD_FLATHASH}
493-
-DFLATBUFFERS_BUILD_FLATLIB=${FLATBUFFERS_BUILD_FLATLIB}
494-
-DFLATBUFFERS_BUILD_TESTS=${FLATBUFFERS_BUILD_TESTS}
495-
-DFLATBUFFERS_INSTALL=${FLATBUFFERS_INSTALL}
496-
-DCMAKE_CXX_FLAGS="-DFLATBUFFERS_MAX_ALIGNMENT=${FLATBUFFERS_MAX_ALIGNMENT}"
497-
# If building for iOS, "unset" these variables to rely on the host (macOS) defaults.
498-
$<$<AND:$<BOOL:${CMAKE_TOOLCHAIN_IOS}>,$<BOOL:$<FILTER:${PLATFORM},EXCLUDE,^MAC>>>:-DCMAKE_OSX_SYSROOT=>
499-
INSTALL_COMMAND ""
500-
BUILD_BYPRODUCTS <BINARY_DIR>/flatc
501-
)
502-
ExternalProject_Get_Property(flatbuffers BINARY_DIR)
503-
if(WIN32)
504-
# flatbuffers does not use CMAKE_BUILD_TYPE. Internally, the build forces Release
505-
# config, but from CMake's perspective the build type is always Debug.
506-
set(FLATC_EXECUTABLE ${BINARY_DIR}/$<CONFIG>/flatc.exe)
507-
elseif(CMAKE_GENERATOR STREQUAL "Xcode")
508-
set(FLATC_EXECUTABLE ${BINARY_DIR}/$<CONFIG>/flatc)
509-
else()
510-
set(FLATC_EXECUTABLE ${BINARY_DIR}/flatc)
511-
endif()
512-
set(FLATC_EXECUTABLE_BUILT_FROM_SOURCE YES)
513-
endif()
514-
515-
if(NOT FLATC_EXECUTABLE)
516-
message(
517-
WARNING "FLATC_EXECUTABLE not specified, looking for flatc"
518-
)
519-
find_program(FLATC_EXECUTABLE flatc)
520-
521-
if(NOT FLATC_EXECUTABLE)
522-
message(FATAL_ERROR "FLATC_EXECUTABLE must be set when EXECUTORCH_BUILD_FLATC is disabled.")
523-
endif()
524-
endif()
525-
526-
add_executable(flatc IMPORTED GLOBAL)
527-
set_target_properties(flatc PROPERTIES IMPORTED_LOCATION ${FLATC_EXECUTABLE})
528-
529-
if(FLATC_EXECUTABLE_BUILT_FROM_SOURCE)
530-
add_dependencies(flatc flatbuffers)
531-
endif()
532460

533461
#
534462
# program_schema: Generated .h files from schema/*.fbs inputs

backends/apple/mps/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ endforeach()
4242
add_custom_command(
4343
OUTPUT ${_mps_schema__outputs}
4444
COMMAND
45-
${FLATC_EXECUTABLE} --cpp --cpp-std c++11 --scoped-enums -o
45+
flatc --cpp --cpp-std c++11 --scoped-enums -o
4646
"${_mps_schema__include_dir}/executorch/backends/apple/mps"
4747
${_mps_schema__srcs}
4848
WORKING_DIRECTORY ${EXECUTORCH_ROOT}

backends/qualcomm/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ set(qcir_schema_include_dir ${CMAKE_CURRENT_LIST_DIR}/aot/ir)
4343
set(qcir_schema_output ${qcir_schema_include_dir}/qcir_generated.h)
4444
add_custom_command(
4545
OUTPUT qcir_schema_output
46-
COMMAND ${FLATC_EXECUTABLE} --cpp --cpp-std c++11 --scoped-enums -o
46+
COMMAND flatc --cpp --cpp-std c++11 --scoped-enums -o
4747
${qcir_schema_include_dir} ${qcir_schema_include_dir}/qcir.fbs
4848
DEPENDS flatc
4949
COMMENT "Generating qualcomm ir schema headers"
@@ -94,7 +94,7 @@ endforeach()
9494
add_custom_command(
9595
OUTPUT ${_qnn_schema__outputs}
9696
COMMAND
97-
${FLATC_EXECUTABLE} --cpp --cpp-std c++11 --scoped-enums -o
97+
flatc --cpp --cpp-std c++11 --scoped-enums -o
9898
"${_qnn_schema__include_dir}/executorch/backends/qualcomm"
9999
${_qnn_schema__srcs}
100100
WORKING_DIRECTORY ${EXECUTORCH_SOURCE_DIR}

backends/vulkan/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ set(GENERATED_HEADER
8585
add_custom_command(
8686
OUTPUT ${GENERATED_HEADER}
8787
COMMAND
88-
${FLATC_EXECUTABLE} --cpp --cpp-std c++11 --scoped-enums -o
88+
flatc --cpp --cpp-std c++11 --scoped-enums -o
8989
"${SCHEMA_INCLUDE_DIR}/executorch/backends/vulkan/serialization/" ${_vulkan_schema__srcs}
9090
WORKING_DIRECTORY ${EXECUTORCH_ROOT}
9191
DEPENDS flatc

backends/xnnpack/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ endif()
9292
add_custom_command(
9393
OUTPUT ${_xnnpack_schema__outputs}
9494
COMMAND
95-
${FLATC_EXECUTABLE} --cpp --cpp-std c++11 --scoped-enums -o
95+
flatc --cpp --cpp-std c++11 --scoped-enums -o
9696
"${_xnnpack_schema__include_dir}/executorch/backends/xnnpack/serialization"
9797
${_xnnpack_schema__srcs}
9898
COMMAND ${MV_COMMAND}

devtools/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ target_link_libraries(
199199
add_custom_command(
200200
OUTPUT ${_bundled_program_schema__outputs}
201201
COMMAND
202-
${FLATC_EXECUTABLE} --cpp --cpp-std c++11 --gen-mutable --scoped-enums -o
202+
flatc --cpp --cpp-std c++11 --gen-mutable --scoped-enums -o
203203
"${_bundled_schema__include_dir}/executorch/devtools/bundled_program/schema"
204204
${_bundled_program_schema__srcs}
205205
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/devtools

extension/flat_tensor/serialize/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function(generate_flat_tensor_schema _schema_srcs _schema_name)
3030
add_custom_command(
3131
OUTPUT ${_schema_outputs}
3232
COMMAND
33-
${FLATC_EXECUTABLE} --cpp --cpp-std c++11 --gen-mutable --scoped-enums -o
33+
flatc --cpp --cpp-std c++11 --gen-mutable --scoped-enums -o
3434
"${_flat_tensor_schema__output_dir}" ${_schema_srcs}
3535
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
3636
DEPENDS flatc ${_schema_srcs}
@@ -45,7 +45,7 @@ function(generate_flat_tensor_schema _schema_srcs _schema_name)
4545
# and some users need an alignment larger than the default, which is typically
4646
# 32.
4747
target_compile_definitions(
48-
${_schema_name} INTERFACE FLATBUFFERS_MAX_ALIGNMENT=${FLATBUFFERS_MAX_ALIGNMENT}
48+
${_schema_name} INTERFACE FLATBUFFERS_MAX_ALIGNMENT=${EXECUTORCH_FLATBUFFERS_MAX_ALIGNMENT}
4949
)
5050

5151
target_include_directories(

schema/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function(generate_program_schema _schema_srcs _schema_name)
3030
add_custom_command(
3131
OUTPUT ${_schema_outputs}
3232
COMMAND
33-
${FLATC_EXECUTABLE} --cpp --cpp-std c++11 --gen-mutable --scoped-enums -o
33+
flatc --cpp --cpp-std c++11 --gen-mutable --scoped-enums -o
3434
"${_program_schema__output_dir}" ${_schema_srcs}
3535
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
3636
DEPENDS flatc ${_schema_srcs}
@@ -45,7 +45,7 @@ function(generate_program_schema _schema_srcs _schema_name)
4545
# and some users need an alignment larger than the default, which is typically
4646
# 32.
4747
target_compile_definitions(
48-
${_schema_name} INTERFACE FLATBUFFERS_MAX_ALIGNMENT=${FLATBUFFERS_MAX_ALIGNMENT}
48+
${_schema_name} INTERFACE FLATBUFFERS_MAX_ALIGNMENT=${EXECUTORCH_FLATBUFFERS_MAX_ALIGNMENT}
4949
)
5050

5151
target_include_directories(

setup.py

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,6 @@ def llama_custom_ops(cls) -> bool:
156156
"EXECUTORCH_BUILD_KERNELS_CUSTOM_AOT", default=True
157157
)
158158

159-
@classmethod
160-
def flatc(cls) -> bool:
161-
return cls._is_cmake_arg_enabled("EXECUTORCH_BUILD_FLATC", default=True)
162-
163159

164160
class Version:
165161
"""Static strings that describe the version of the pip package."""
@@ -833,23 +829,20 @@ def run(self):
833829

834830
def get_ext_modules() -> List[Extension]:
835831
"""Returns the set of extension modules to build."""
836-
ext_modules = []
837-
if ShouldBuild.flatc():
838-
ext_modules.extend(
839-
[
840-
BuiltFile(
841-
src_dir="%CMAKE_CACHE_DIR%/third-party/flatbuffers/%BUILD_TYPE%/",
842-
src_name="flatc",
843-
dst="executorch/data/bin/",
844-
is_executable=True,
845-
),
846-
BuiltFile(
847-
src_dir="tools/wheel",
848-
src_name="pip_data_bin_init.py.in",
849-
dst="executorch/data/bin/__init__.py",
850-
),
851-
]
852-
)
832+
833+
ext_modules = [
834+
BuiltFile(
835+
src_dir="%CMAKE_CACHE_DIR%/third-party/flatbuffers_external_project",
836+
src_name="flatc",
837+
dst="executorch/data/bin/",
838+
is_executable=True,
839+
),
840+
BuiltFile(
841+
src_dir="tools/wheel",
842+
src_name="pip_data_bin_init.py.in",
843+
dst="executorch/data/bin/__init__.py",
844+
),
845+
]
853846

854847
if ShouldBuild.pybindings():
855848
ext_modules.append(

third-party/CMakeLists.txt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
# MARK: - flatbuffers
9+
10+
# We use ExternalProject to build flatc from source to force it target the host.
11+
# Otherwise, flatc will target the project's toolchain (i.e. iOS, or Android).
12+
ExternalProject_Add(
13+
flatbuffers_external_project
14+
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/flatbuffers_external_project
15+
BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/flatbuffers_external_project
16+
SOURCE_DIR ${PROJECT_SOURCE_DIR}/third-party/flatbuffers
17+
# Always use Make to avoid needing to codesign flatc if the project is using Xcode.
18+
CMAKE_GENERATOR "Unix Makefiles"
19+
CMAKE_ARGS -DFLATBUFFERS_BUILD_FLATC=ON
20+
-DFLATBUFFERS_INSTALL=OFF
21+
-DFLATBUFFERS_BUILD_FLATHASH=OFF
22+
-DFLATBUFFERS_BUILD_FLATLIB=OFF
23+
-DFLATBUFFERS_BUILD_TESTS=OFF
24+
-DCMAKE_CXX_FLAGS="-DFLATBUFFERS_MAX_ALIGNMENT=${EXECUTORCH_FLATBUFFERS_MAX_ALIGNMENT}"
25+
# Unset the toolchain to build for the host instead of the toolchain set for the project.
26+
-DCMAKE_TOOLCHAIN_FILE=
27+
# If building for iOS, "unset" these variables to rely on the host (macOS) defaults.
28+
$<$<AND:$<BOOL:${APPLE}>,$<BOOL:$<FILTER:${PLATFORM},EXCLUDE,^MAC>>>:-DCMAKE_OSX_SYSROOT=>
29+
-DCMAKE_OSX_DEPLOYMENT_TARGET:STRING=${CMAKE_OSX_DEPLOYMENT_TARGET}
30+
INSTALL_COMMAND ""
31+
BUILD_BYPRODUCTS <BINARY_DIR>/flatc
32+
)
33+
ExternalProject_Get_Property(flatbuffers_external_project BINARY_DIR)
34+
add_executable(flatc IMPORTED GLOBAL)
35+
add_dependencies(flatc flatbuffers_external_project)
36+
if(WIN32)
37+
# flatbuffers does not use CMAKE_BUILD_TYPE. Internally, the build forces Release
38+
# config, but from CMake's perspective the build type is always Debug.
39+
set_target_properties(flatc PROPERTIES IMPORTED_LOCATION ${BINARY_DIR}/$<CONFIG>/flatc.exe)
40+
else()
41+
set_target_properties(flatc PROPERTIES IMPORTED_LOCATION ${BINARY_DIR}/flatc)
42+
endif()
43+
44+
# TODO: re-enable once flatbuffers is added as a subdirectory.
45+
# set(FLATBUFFERS_BUILD_FLATC OFF)
46+
# set(FLATBUFFERS_INSTALL OFF)
47+
# set(FLATBUFFERS_BUILD_FLATHASH OFF)
48+
# set(FLATBUFFERS_BUILD_FLATLIB OFF)
49+
# set(FLATBUFFERS_BUILD_TESTS OFF)

tools/cmake/Utils.cmake

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ function(executorch_print_configuration_summary)
2929
message(STATUS " CMAKE_TOOLCHAIN_FILE : ${CMAKE_TOOLCHAIN_FILE}")
3030
message(STATUS " BUCK2 : ${BUCK2}")
3131
message(STATUS " PYTHON_EXECUTABLE : ${PYTHON_EXECUTABLE}")
32-
message(STATUS " FLATC_EXECUTABLE : ${FLATC_EXECUTABLE}")
3332
message(STATUS " EXECUTORCH_ENABLE_PROGRAM_VERIFICATION : "
3433
"${EXECUTORCH_ENABLE_PROGRAM_VERIFICATION}"
3534
)
@@ -77,10 +76,6 @@ function(executorch_print_configuration_summary)
7776
message(STATUS " EXECUTORCH_BUILD_EXTENSION_TRAINING : "
7877
"${EXECUTORCH_BUILD_EXTENSION_TRAINING}"
7978
)
80-
message(
81-
STATUS
82-
" EXECUTORCH_BUILD_FLATC : ${EXECUTORCH_BUILD_FLATC}"
83-
)
8479
message(
8580
STATUS
8681
" EXECUTORCH_BUILD_GFLAGS : ${EXECUTORCH_BUILD_GFLAGS}"

tools/cmake/preset/default.cmake

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ else()
1212
set(_is_build_type_debug ON)
1313
endif()
1414

15-
# MARK: - Definitions
15+
# MARK: - Overridable Options
1616

1717
define_overridable_option(EXECUTORCH_ENABLE_LOGGING "Build with ET_LOG_ENABLED" BOOL ${_is_build_type_debug})
1818
define_overridable_option(EXECUTORCH_BUILD_COREML "Build the Core ML backend" BOOL OFF)
19+
define_overridable_option(
20+
EXECUTORCH_FLATBUFFERS_MAX_ALIGNMENT
21+
"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."
22+
STRING 1024
23+
)

0 commit comments

Comments
 (0)