diff --git a/CMake/BuildFlatBuffers.cmake b/CMake/BuildFlatBuffers.cmake index 2fc052957f98..a17e23c71913 100644 --- a/CMake/BuildFlatBuffers.cmake +++ b/CMake/BuildFlatBuffers.cmake @@ -175,15 +175,14 @@ function(build_flatbuffers flatbuffers_schemas endif() endfunction() -# Creates a target that can be linked against that generates flatbuffer headers. +# Creates a target that can be linked against that provides compiled versions of flatbuffer schemas. # -# This function takes a target name and a list of schemas. You can also specify -# other flagc flags using the FLAGS option to change the behavior of the flatc -# tool. +# This function takes a target name and a list of schemas. Custom commands will be created +# to generate the schemas, such that linking to the target passed as the TARGET argument +# will make the schema headers available. # -# When the target_link_libraries is done within a different directory than -# flatbuffers_generate_headers is called, then the target should also be dependent -# the custom generation target called GENERATE_. +# You can also specify other flagc flags using the FLAGS option to change the behavior of the flatc +# tool. # # Arguments: # TARGET: The name of the target to generate. @@ -211,8 +210,6 @@ endfunction() # PRIVATE my_generated_headers_target # ) # -# Optional (only needed within different directory): -# add_dependencies(app GENERATE_my_generated_headers_target) function(flatbuffers_generate_headers) # Parse function arguments. set(options) @@ -315,7 +312,13 @@ function(flatbuffers_generate_headers) # Set up interface library. # This library is for users to link to, and depends on the custom target (so all the custom commands get run). # It also adds the appropriate include paths. - add_library(${FLATBUFFERS_GENERATE_HEADERS_TARGET} INTERFACE) + # If there are no source files we use an interface library, otherwise compile the source files into a static lib. + if("${all_generated_source_files}" STREQUAL "") + add_library(${FLATBUFFERS_GENERATE_HEADERS_TARGET} INTERFACE) + else() + add_library(${FLATBUFFERS_GENERATE_HEADERS_TARGET} STATIC ${all_generated_source_files}) + endif() + add_dependencies( ${FLATBUFFERS_GENERATE_HEADERS_TARGET} ${generate_target}) diff --git a/CMake/FindFlatBuffers.cmake b/CMake/FindFlatBuffers.cmake index c3253eab85c3..0c085576429d 100644 --- a/CMake/FindFlatBuffers.cmake +++ b/CMake/FindFlatBuffers.cmake @@ -23,6 +23,9 @@ # # If flatc is found, the following imported target is created: # * flatbuffers::flatc - Imported target for the compiler +# +# If flatc is found, the following function is provided to compile the schemas into C headers: +# * flatbuffers_generate_headers() [see BuildFlatBuffers.cmake for signature] set(FLATBUFFERS_CMAKE_DIR ${CMAKE_CURRENT_LIST_DIR}) @@ -58,7 +61,7 @@ if(FlatBuffers_FOUND) # LEGACY function for generating C headers from a flatbuffer. # Deprecated, use flatbuffers_generate_headers() from BuildFlatBuffers.cmake instead, - # which allows passing options and generating C++ targets + # which allows passing options and generating library targets function(FLATBUFFERS_GENERATE_C_HEADERS Name) set(FLATC_OUTPUTS) foreach(FILE ${ARGN}) @@ -76,6 +79,6 @@ if(FlatBuffers_FOUND) endforeach() set(${Name}_OUTPUTS ${FLATC_OUTPUTS} PARENT_SCOPE) endfunction() -endif() -include("${FLATBUFFERS_CMAKE_DIR}/BuildFlatBuffers.cmake") + include("${FLATBUFFERS_CMAKE_DIR}/BuildFlatBuffers.cmake") +endif() diff --git a/CMakeLists.txt b/CMakeLists.txt index 69629f151cb6..3f7c15b03071 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -86,7 +86,7 @@ endif() if(NOT FLATBUFFERS_BUILD_FLATC) # If we aren't supposed to build flatc, see if we can find it. find_package(FlatBuffers ${FLATBUFFERS_VERSION_NOCOMMIT}) - set(FLATBUFFERS_FLATC_TARGET ${FLATBUFFERS_FLATC_EXECUTABLE}) + set(FLATBUFFERS_FLATC_COMMAND ${FLATBUFFERS_FLATC_EXECUTABLE}) if(NOT FlatBuffers_FOUND AND FLATBUFFERS_BUILD_TESTS) message(WARNING @@ -469,7 +469,7 @@ if(FLATBUFFERS_BUILD_FLATC) endif() if(NOT FLATBUFFERS_FLATC_EXECUTABLE) set(FLATBUFFERS_FLATC_EXECUTABLE $) - set(FLATBUFFERS_FLATC_TARGET flatc) + set(FLATBUFFERS_FLATC_COMMAND flatc) endif() if(FLATBUFFERS_STATIC_FLATC AND NOT MSVC) target_link_libraries(flatc PRIVATE -static) @@ -506,7 +506,7 @@ function(compile_schema SRC_FBS OPT OUT_GEN_FILE) ${OPT} -o "${SRC_FBS_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}" - DEPENDS ${FLATBUFFERS_FLATC_TARGET} ${SRC_FBS} + DEPENDS ${FLATBUFFERS_FLATC_COMMAND} ${SRC_FBS} COMMENT "flatc generation: `${SRC_FBS}` -> `${GEN_HEADER}`" ) set(${OUT_GEN_FILE} ${GEN_HEADER} PARENT_SCOPE)