diff options
Diffstat (limited to 'CMakeLists.txt')
-rw-r--r-- | CMakeLists.txt | 170 |
1 files changed, 33 insertions, 137 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 53f79db5..885984db 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,7 +21,7 @@ option(FLATBUFFERS_BUILD_FLATC "Enable the build of the flatbuffers compiler" ON) option(FLATBUFFERS_STATIC_FLATC "Build flatbuffers compiler with -static flag" OFF) -option(FLATBUFFERS_BUILD_FLATHASH "Enable the build of flathash" ON) +option(FLATBUFFERS_BUILD_FLATHASH "Enable the build of flathash" OFF) option(FLATBUFFERS_BUILD_BENCHMARKS "Enable the build of flatbenchmark." OFF) option(FLATBUFFERS_BUILD_GRPCTEST "Enable the build of grpctest" OFF) @@ -243,32 +243,21 @@ set(FlatBuffers_Tests_CPP17_SRCS tests/test_assert.h tests/test_assert.cpp tests/cpp17/test_cpp17.cpp - # file generate by running compiler on tests/monster_test.fbs - ${CMAKE_CURRENT_BINARY_DIR}/tests/cpp17/generated_cpp17/monster_test_generated.h - ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h - ${CMAKE_CURRENT_BINARY_DIR}/tests/cpp17/generated_cpp17/optional_scalars_generated.h - ${CMAKE_CURRENT_BINARY_DIR}/tests/optional_scalars_generated.h ) set(FlatBuffers_Sample_Binary_SRCS include/flatbuffers/flatbuffers.h samples/sample_binary.cpp - # file generated by running compiler on samples/monster.fbs - ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h ) set(FlatBuffers_Sample_Text_SRCS ${FlatBuffers_Library_SRCS} samples/sample_text.cpp - # file generated by running compiler on samples/monster.fbs - ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h ) set(FlatBuffers_Sample_BFBS_SRCS ${FlatBuffers_Library_SRCS} samples/sample_bfbs.cpp - # file generated by running compiler on samples/monster.fbs - ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h ) set(FlatBuffers_GRPCTest_SRCS @@ -284,8 +273,6 @@ set(FlatBuffers_GRPCTest_SRCS tests/test_builder.cpp grpc/tests/grpctest.cpp grpc/tests/message_builder_test.cpp - # file generate by running compiler on tests/monster_test.fbs - ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h ) # TODO(dbaileychess): Figure out how this would now work. I posted a question on @@ -500,144 +487,64 @@ if(FLATBUFFERS_BUILD_SHAREDLIB) endif() endif() -# Global list of generated files. -# Use the global property to be independent of PARENT_SCOPE. -set_property(GLOBAL PROPERTY FBS_GENERATED_OUTPUTS) - -function(get_generated_output generated_files) - get_property(tmp GLOBAL PROPERTY FBS_GENERATED_OUTPUTS) - set(${generated_files} ${tmp} PARENT_SCOPE) -endfunction(get_generated_output) - -function(register_generated_output file_name) - get_property(tmp GLOBAL PROPERTY FBS_GENERATED_OUTPUTS) - list(APPEND tmp ${file_name}) - set_property(GLOBAL PROPERTY FBS_GENERATED_OUTPUTS ${tmp}) -endfunction(register_generated_output) - -function(compile_flatbuffers_schema_to_cpp_opt SRC_FBS OPT) - if(FLATBUFFERS_BUILD_LEGACY) - set(OPT ${OPT};--cpp-std c++0x) - else() - # --cpp-std is defined by flatc default settings. - endif() - message(STATUS "`${SRC_FBS}`: add generation of C++ code with '${OPT}'") +function(compile_schema SRC_FBS OPT OUT_GEN_FILE) get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH) string(REGEX REPLACE "\\.fbs$" "_generated.h" GEN_HEADER ${SRC_FBS}) - add_custom_command( - OUTPUT ${GEN_HEADER} - COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}" - --cpp --gen-mutable --gen-object-api --reflect-names - --cpp-ptr-type flatbuffers::unique_ptr # Used to test with C++98 STLs - ${OPT} - -I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test" - -o "${SRC_FBS_DIR}" - "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}" - DEPENDS flatc - COMMENT "Run generation: '${GEN_HEADER}'") - register_generated_output(${GEN_HEADER}) -endfunction() - -function(compile_flatbuffers_schema_to_cpp SRC_FBS) - compile_flatbuffers_schema_to_cpp_opt(${SRC_FBS} "--no-includes;--gen-compare") + add_custom_command(TARGET flatc POST_BUILD + COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}" + ${OPT} + -o "${SRC_FBS_DIR}" + "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}" + BYPRODUCTS ${GEN_HEADER} + COMMENT "flatc generation: `${SRC_FBS}` -> `${GEN_HEADER}`" + ) + set(${OUT_GEN_FILE} ${GEN_HEADER} PARENT_SCOPE) endfunction() -function(compile_flatbuffers_schema_to_binary SRC_FBS) - message(STATUS "`${SRC_FBS}`: add generation of binary (.bfbs) schema") - get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH) - string(REGEX REPLACE "\\.fbs$" ".bfbs" GEN_BINARY_SCHEMA ${SRC_FBS}) - # For details about flags see generate_code.py - add_custom_command( - OUTPUT ${GEN_BINARY_SCHEMA} - COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}" - -b --schema --bfbs-comments --bfbs-builtins - --bfbs-filenames "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS_DIR}" - -I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test" - -o "${SRC_FBS_DIR}" - "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}" - DEPENDS flatc - COMMENT "Run generation: '${GEN_BINARY_SCHEMA}'") - register_generated_output(${GEN_BINARY_SCHEMA}) +function(compile_schema_for_test SRC_FBS OPT) + compile_schema("${SRC_FBS}" "${OPT}" GEN_FILE) + target_sources(flattests PRIVATE ${GEN_FILE}) endfunction() -function(compile_flatbuffers_schema_to_embedded_binary SRC_FBS OPT) - if(FLATBUFFERS_BUILD_LEGACY) - set(OPT ${OPT};--cpp-std c++0x) - else() - # --cpp-std is defined by flatc default settings. - endif() - message(STATUS "`${SRC_FBS}`: add generation of C++ embedded binary schema code with '${OPT}'") - get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH) - string(REGEX REPLACE "\\.fbs$" "_bfbs_generated.h" GEN_BFBS_HEADER ${SRC_FBS}) - # For details about flags see generate_code.py - add_custom_command( - OUTPUT ${GEN_BFBS_HEADER} - COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}" - --cpp --gen-mutable --gen-object-api --reflect-names - --cpp-ptr-type flatbuffers::unique_ptr # Used to test with C++98 STLs - ${OPT} - --bfbs-comments --bfbs-builtins --bfbs-gen-embed - --bfbs-filenames ${SRC_FBS_DIR} - -I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test" - -o "${SRC_FBS_DIR}" - "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}" - DEPENDS flatc - COMMENT "Run generation: '${GEN_BFBS_HEADER}'") - register_generated_output(${GEN_BFBS_HEADER}) +function(compile_schema_for_samples SRC_FBS OPT) + compile_schema("${SRC_FBS}" "${OPT}" GEN_FILE) + target_sources(flatsamplebinary PRIVATE ${GEN_FILE}) + target_sources(flatsampletext PRIVATE ${GEN_FILE}) + target_sources(flatsamplebfbs PRIVATE ${GEN_FILE}) endfunction() if(FLATBUFFERS_BUILD_TESTS) - file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/tests" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}") - file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/samples" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}") - - # TODO Add (monster_test.fbs monsterdata_test.json)->monsterdata_test.mon - compile_flatbuffers_schema_to_binary(tests/monster_test.fbs) - compile_flatbuffers_schema_to_cpp_opt(tests/namespace_test/namespace_test1.fbs "--no-includes;--gen-compare;--gen-name-strings") - compile_flatbuffers_schema_to_cpp_opt(tests/namespace_test/namespace_test2.fbs "--no-includes;--gen-compare;--gen-name-strings") - compile_flatbuffers_schema_to_cpp_opt(tests/union_vector/union_vector.fbs "--no-includes;--gen-compare;") - compile_flatbuffers_schema_to_cpp(tests/optional_scalars.fbs) - compile_flatbuffers_schema_to_cpp_opt(tests/native_type_test.fbs "") - compile_flatbuffers_schema_to_cpp_opt(tests/arrays_test.fbs "--scoped-enums;--gen-compare") - compile_flatbuffers_schema_to_binary(tests/arrays_test.fbs) - compile_flatbuffers_schema_to_embedded_binary(tests/monster_test.fbs "--no-includes;--gen-compare") - compile_flatbuffers_schema_to_cpp(tests/native_inline_table_test.fbs "--gen-compare") - compile_flatbuffers_schema_to_cpp(tests/alignment_test.fbs "--gen-compare") - compile_flatbuffers_schema_to_cpp(tests/key_field/key_field_sample.fbs) - if(NOT (MSVC AND (MSVC_VERSION LESS 1900))) - compile_flatbuffers_schema_to_cpp(tests/monster_extra.fbs) # Test floating-point NAN/INF. - endif() include_directories(${CMAKE_CURRENT_BINARY_DIR}/tests) add_executable(flattests ${FlatBuffers_Tests_SRCS}) target_link_libraries(flattests PRIVATE $<BUILD_INTERFACE:ProjectConfig>) - target_include_directories(flattests PUBLIC src) - add_dependencies(flattests generated_code) + + # The flattest target needs some generated files + SET(FLATC_OPT --cpp --gen-mutable --gen-object-api --reflect-names) + SET(FLATC_OPT_COMP ${FLATC_OPT};--gen-compare) + + compile_schema_for_test(tests/alignment_test.fbs "${FLATC_OPT_COMP}") + compile_schema_for_test(tests/native_inline_table_test.fbs "${FLATC_OPT_COMP}") + compile_schema_for_test(tests/native_type_test.fbs "${FLATC_OPT}") if(FLATBUFFERS_CODE_SANITIZE) add_fsanitize_to_target(flattests ${FLATBUFFERS_CODE_SANITIZE}) endif() - - compile_flatbuffers_schema_to_cpp(samples/monster.fbs) - compile_flatbuffers_schema_to_binary(samples/monster.fbs) + include_directories(${CMAKE_CURRENT_BINARY_DIR}/samples) add_executable(flatsamplebinary ${FlatBuffers_Sample_Binary_SRCS}) - target_link_libraries(flatsamplebinary PRIVATE $<BUILD_INTERFACE:ProjectConfig>) - add_dependencies(flatsamplebinary generated_code) - add_executable(flatsampletext ${FlatBuffers_Sample_Text_SRCS}) - target_link_libraries(flatsampletext PRIVATE $<BUILD_INTERFACE:ProjectConfig>) - add_dependencies(flatsampletext generated_code) - add_executable(flatsamplebfbs ${FlatBuffers_Sample_BFBS_SRCS}) + + target_link_libraries(flatsamplebinary PRIVATE $<BUILD_INTERFACE:ProjectConfig>) + target_link_libraries(flatsampletext PRIVATE $<BUILD_INTERFACE:ProjectConfig>) target_link_libraries(flatsamplebfbs PRIVATE $<BUILD_INTERFACE:ProjectConfig>) - add_dependencies(flatsamplebfbs generated_code) + + compile_schema_for_samples(samples/monster.fbs "${FLATC_OPT_COMP}") if(FLATBUFFERS_BUILD_CPP17) - # Don't generate header for flattests_cpp17 target. - # This target uses "generated_cpp17/monster_test_generated.h" add_executable(flattests_cpp17 ${FlatBuffers_Tests_CPP17_SRCS}) - add_dependencies(flattests_cpp17 generated_code) target_link_libraries(flattests_cpp17 PRIVATE $<BUILD_INTERFACE:ProjectConfig>) target_compile_features(flattests_cpp17 PRIVATE cxx_std_17) # requires cmake 3.8 @@ -662,7 +569,6 @@ if(FLATBUFFERS_BUILD_GRPCTEST) find_package(protobuf CONFIG REQUIRED) find_package(gRPC CONFIG REQUIRED) add_executable(grpctest ${FlatBuffers_GRPCTest_SRCS}) - add_dependencies(grpctest generated_code) target_link_libraries(grpctext PRIVATE $<BUILD_INTERFACE:ProjectConfig> @@ -755,16 +661,6 @@ if(FLATBUFFERS_BUILD_TESTS) endif() endif() -# This target is sync-barrier. -# Other generate-dependent targets can depend on 'generated_code' only. -get_generated_output(fbs_generated) -if(fbs_generated) - # message(STATUS "Add generated_code target with files:${fbs_generated}") - add_custom_target(generated_code - DEPENDS ${fbs_generated} - COMMENT "All generated files were updated.") -endif() - include(CMake/BuildFlatBuffers.cmake) if(UNIX) |