Skip to content

Commit

Permalink
[infra/fb] Introduce FlatBuffersMuteable_Target (#13687)
Browse files Browse the repository at this point in the history
This will introduce FlatBuffersMuteable_Target method to produce schema
generated header with mutable methods.

ONE-DCO-1.0-Signed-off-by: SaeHie Park <[email protected]>
  • Loading branch information
seanshpark authored Aug 19, 2024
1 parent d82e145 commit 8f98b9a
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions infra/cmake/packages/FlatBuffers-23.5.26/FlatBuffersConfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 8f98b9a

Please sign in to comment.