From 3f8700b29669855ae440ad67180742ede82b3fb2 Mon Sep 17 00:00:00 2001 From: Zbigniew Mandziejewicz <shaxbee@gmail.com> Date: Tue, 16 Sep 2014 00:50:23 +0800 Subject: [PATCH] Generate headers from spec, add option for installing targets Change-Id: I16ddb06e887e618fa871e842054115909fbf235c --- CMakeLists.txt | 42 +++++++++++++++++++++++++------------ samples/monster_generated.h | 4 ++-- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 68bfb601..96de9a09 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,7 @@ project(FlatBuffers) # NOTE: Code coverage only works on Linux & OSX. option(FLATBUFFERS_CODE_COVERAGE "Enable the code coverage build option." OFF) option(FLATBUFFERS_BUILD_TESTS "Enable the build of tests and samples." ON) +option(FLATBUFFERS_INSTALL "Enable the installation of targets." ON) set(FlatBuffers_Compiler_SRCS include/flatbuffers/flatbuffers.h @@ -26,14 +27,14 @@ set(FlatBuffers_Tests_SRCS src/idl_gen_text.cpp tests/test.cpp # file generate by running compiler on tests/monster_test.fbs - tests/monster_test_generated.h + ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h ) set(FlatBuffers_Sample_Binary_SRCS include/flatbuffers/flatbuffers.h samples/sample_binary.cpp - # file generate by running compiler on samples/monster.fbs - samples/monster_generated.h + # file generated by running compiler on samples/monster.fbs + ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h ) set(FlatBuffers_Sample_Text_SRCS @@ -43,12 +44,10 @@ set(FlatBuffers_Sample_Text_SRCS src/idl_parser.cpp src/idl_gen_text.cpp samples/sample_text.cpp - # file generate by running compiler on samples/monster.fbs - samples/monster_generated.h + # file generated by running compiler on samples/monster.fbs + ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h ) -set(CMAKE_BUILD_TYPE Debug) - # source_group(Compiler FILES ${FlatBuffers_Compiler_SRCS}) # source_group(Tests FILES ${FlatBuffers_Tests_SRCS}) @@ -69,18 +68,35 @@ endif() include_directories(include) add_executable(flatc ${FlatBuffers_Compiler_SRCS}) + +function(compile_flatbuffers_schema_to_cpp SRC_FBS) + get_filename_component(SRC_FBS_DIR ${SRC_FBS} DIRECTORY) + string(REGEX REPLACE ".fbs$" "_generated.h" GEN_HEADER ${SRC_FBS}) + add_custom_command( + OUTPUT ${GEN_HEADER} + COMMAND flatc -c -o "${SRC_FBS_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}" + DEPENDS flatc) +endfunction() + if(FLATBUFFERS_BUILD_TESTS) + compile_flatbuffers_schema_to_cpp(tests/monster_test.fbs) + include_directories(${CMAKE_CURRENT_BINARY_DIR}/tests) add_executable(flattests ${FlatBuffers_Tests_SRCS}) + + compile_flatbuffers_schema_to_cpp(samples/monster.fbs) + include_directories(${CMAKE_CURRENT_BINARY_DIR}/samples) add_executable(flatsamplebinary ${FlatBuffers_Sample_Binary_SRCS}) add_executable(flatsampletext ${FlatBuffers_Sample_Text_SRCS}) endif() -install(DIRECTORY include/flatbuffers DESTINATION include) -install(TARGETS flatc DESTINATION bin) +if(FLATBUFFERS_INSTALL) + install(DIRECTORY include/flatbuffers DESTINATION include) + install(TARGETS flatc DESTINATION bin) +endif() if(FLATBUFFERS_BUILD_TESTS) - add_test(NAME flattest - CONFIGURATIONS Debug - WORKING_DIRECTORY tests - COMMAND flattests) + enable_testing() + + file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/tests" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}") + add_test(NAME flattests COMMAND flattests) endif() diff --git a/samples/monster_generated.h b/samples/monster_generated.h index b2a44e65..dc460382 100755 --- a/samples/monster_generated.h +++ b/samples/monster_generated.h @@ -37,7 +37,7 @@ inline const char **EnumNamesAny() { inline const char *EnumNameAny(int e) { return EnumNamesAny()[e]; } -bool VerifyAny(flatbuffers::Verifier &verifier, const void *union_obj, uint8_t type); +inline bool VerifyAny(flatbuffers::Verifier &verifier, const void *union_obj, uint8_t type); MANUALLY_ALIGNED_STRUCT(4) Vec3 { private: @@ -107,7 +107,7 @@ inline flatbuffers::Offset<Monster> CreateMonster(flatbuffers::FlatBufferBuilder return builder_.Finish(); } -bool VerifyAny(flatbuffers::Verifier &verifier, const void *union_obj, uint8_t type) { +inline bool VerifyAny(flatbuffers::Verifier &verifier, const void *union_obj, uint8_t type) { switch (type) { case Any_NONE: return true; case Any_Monster: return verifier.VerifyTable(reinterpret_cast<const Monster *>(union_obj)); -- GitLab