xref: /aosp_15_r20/external/executorch/schema/CMakeLists.txt (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
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# Flatbuffer schema header lib. Please this file formatted by running:
8# ~~~
9# cmake-format -i CMakeLists.txt
10# ~~~
11
12if(NOT FLATC_EXECUTABLE)
13  set(FLATC_EXECUTABLE flatc)
14endif()
15
16# The include directory that will contain the generated schema headers.
17set(_program_schema__include_dir "${CMAKE_BINARY_DIR}/schema/include")
18
19# Source root directory for executorch.
20if(NOT EXECUTORCH_ROOT)
21  set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/..)
22endif()
23
24function(generate_program_schema _schema_srcs _schema_name)
25  set(_schema_outputs)
26  foreach(fbs_file ${_schema_srcs})
27    string(REGEX REPLACE "[.]fbs$" "_generated.h" generated "${fbs_file}")
28    list(APPEND _schema_outputs
29         "${_program_schema__include_dir}/executorch/${generated}"
30    )
31  endforeach()
32
33  # Generate the headers from the .fbs files.
34  add_custom_command(
35    OUTPUT ${_schema_outputs}
36    COMMAND
37      ${FLATC_EXECUTABLE} --cpp --cpp-std c++11 --gen-mutable --scoped-enums -o
38      "${_program_schema__include_dir}/executorch/schema" ${_schema_srcs}
39    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
40    DEPENDS ${FLATC_EXECUTABLE} ${_schema_srcs}
41    COMMENT "Generating ${_schema_name} headers"
42    VERBATIM
43  )
44
45  add_library(${_schema_name} INTERFACE ${_schema_outputs})
46  set_target_properties(${_schema_name} PROPERTIES LINKER_LANGUAGE CXX)
47
48  # exir lets users set the alignment of tensor data embedded in the flatbuffer,
49  # and some users need an alignment larger than the default, which is typically
50  # 32.
51  target_compile_definitions(
52    ${_schema_name} INTERFACE FLATBUFFERS_MAX_ALIGNMENT=1024
53  )
54
55  target_include_directories(
56    ${_schema_name}
57    INTERFACE ${_program_schema__include_dir}
58              ${EXECUTORCH_ROOT}/third-party/flatbuffers/include
59  )
60endfunction()
61
62# Generate common schema
63set(common_schema_srcs scalar_type.fbs)
64generate_program_schema("${common_schema_srcs}" "common_schema")
65
66# For the other schemas
67set(program_schema_srcs program.fbs)
68generate_program_schema("${program_schema_srcs}" "program_schema")
69add_dependencies(program_schema common_schema)
70