Generate headers from spec, add option for installing targets
authorZbigniew Mandziejewicz <shaxbee@gmail.com>
Mon, 15 Sep 2014 16:50:23 +0000 (00:50 +0800)
committerWouter van Oortmerssen <wvo@google.com>
Mon, 22 Sep 2014 22:53:19 +0000 (15:53 -0700)
Change-Id: I16ddb06e887e618fa871e842054115909fbf235c

CMakeLists.txt
samples/monster_generated.h

index 68bfb60..96de9a0 100644 (file)
@@ -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()
index b2a44e6..dc46038 100755 (executable)
@@ -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));