|
| 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 | + |
| 12 | +if(NOT FLATC_EXECUTABLE) |
| 13 | + set(FLATC_EXECUTABLE flatc) |
| 14 | +endif() |
| 15 | + |
| 16 | +# The include directory that will contain the generated schema headers. |
| 17 | +set(_flat_tensor_schema__include_dir "${CMAKE_BINARY_DIR}/extension/flat_tensor/include") |
| 18 | +set(_flat_tensor_schema__output_dir "${_flat_tensor_schema__include_dir}/executorch/extension/flat_tensor/serialize") |
| 19 | +# Source root directory for executorch. |
| 20 | +if(NOT EXECUTORCH_ROOT) |
| 21 | + set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/..) |
| 22 | +endif() |
| 23 | + |
| 24 | +function(generate_flat_tensor_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 | + "${_flat_tensor_schema__output_dir}/${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 | + "${_flat_tensor_schema__output_dir}" ${_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 ${_flat_tensor_schema__include_dir} |
| 58 | + ${EXECUTORCH_ROOT}/third-party/flatbuffers/include |
| 59 | + ) |
| 60 | +endfunction() |
| 61 | + |
| 62 | +# Generate common schema |
| 63 | +set(scalar_type_schema_srcs scalar_type.fbs) |
| 64 | +generate_flat_tensor_schema("${scalar_type_schema_srcs}" "scalar_type_schema") |
| 65 | + |
| 66 | +# For the other schemas |
| 67 | +set(flat_tensor_schema_srcs flat_tensor.fbs) |
| 68 | +generate_flat_tensor_schema("${flat_tensor_schema_srcs}" "flat_tensor_schema") |
| 69 | +add_dependencies(flat_tensor_schema scalar_type_schema) |
0 commit comments