From 8f98b9aa6b551f01dfdb0b4c38ff637876f025d9 Mon Sep 17 00:00:00 2001 From: SaeHie Park Date: Mon, 19 Aug 2024 14:44:25 +0900 Subject: [PATCH] [infra/fb] Introduce FlatBuffersMuteable_Target (#13687) This will introduce FlatBuffersMuteable_Target method to produce schema generated header with mutable methods. ONE-DCO-1.0-Signed-off-by: SaeHie Park --- .../FlatBuffersConfig.cmake | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/infra/cmake/packages/FlatBuffers-23.5.26/FlatBuffersConfig.cmake b/infra/cmake/packages/FlatBuffers-23.5.26/FlatBuffersConfig.cmake index d98882a32cc..cc77b844d1d 100644 --- a/infra/cmake/packages/FlatBuffers-23.5.26/FlatBuffersConfig.cmake +++ b/infra/cmake/packages/FlatBuffers-23.5.26/FlatBuffersConfig.cmake @@ -132,4 +132,53 @@ if(FlatBuffers_FOUND) target_include_directories(${TGT} PUBLIC "${ARG_INCLUDE_DIR}") target_link_libraries(${TGT} PUBLIC flatbuffers-23.5.26) endfunction(FlatBuffers_Target) + + function(FlatBuffersMuteable_Target TGT) + set(oneValueArgs OUTPUT_DIR SCHEMA_DIR INCLUDE_DIR) + set(multiValueArgs SCHEMA_FILES) + cmake_parse_arguments(ARG "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + # Use OUTPUT_DIR as INCLUDE_DIR if INCLUDE_DIR is not specified + if(NOT ARG_INCLUDE_DIR) + set(ARG_INCLUDE_DIR ${ARG_OUTPUT_DIR}) + endif(NOT ARG_INCLUDE_DIR) + + get_filename_component(abs_output_dir ${ARG_OUTPUT_DIR} ABSOLUTE) + get_filename_component(abs_include_dir ${ARG_INCLUDE_DIR} ABSOLUTE) + get_filename_component(abs_schema_dir ${ARG_SCHEMA_DIR} ABSOLUTE) + + # Let's reset list variables before using them + # NOTE THIS DOES NOT AFFECT parent scope + unset(SCHEMA_FILES) + unset(OUTPUT_FILES) + + foreach(schema ${ARG_SCHEMA_FILES}) + get_filename_component(schema_fn "${schema}" NAME) + get_filename_component(dir "${schema}" DIRECTORY) + + get_filename_component(schema_fn_we "${schema_fn}" NAME_WE) + + list(APPEND SCHEMA_FILES "${abs_schema_dir}/${schema}") + list(APPEND OUTPUT_FILES "${abs_output_dir}/${schema_fn_we}_generated.h") + endforeach() + + # Generate headers + add_custom_command(OUTPUT ${OUTPUT_FILES} + COMMAND ${CMAKE_COMMAND} -E make_directory "${abs_output_dir}" + COMMAND "${FLATC_PATH}" -c --no-includes + --no-union-value-namespacing + --gen-object-api + --gen-mutable + -o "${abs_output_dir}" + ${SCHEMA_FILES} + DEPENDS ${SCHEMA_FILES} + COMMENT "Generate '${TGT}' headers") + + # NOTE This header-only library is deliberately declared as STATIC library + # to avoid possible scope issues related with generated files + add_library(${TGT} STATIC ${OUTPUT_FILES}) + set_target_properties(${TGT} PROPERTIES LINKER_LANGUAGE CXX) + target_include_directories(${TGT} PUBLIC "${ARG_INCLUDE_DIR}") + target_link_libraries(${TGT} PUBLIC flatbuffers-23.5.26) + endfunction(FlatBuffersMuteable_Target) endif(FlatBuffers_FOUND)