1 cmake_minimum_required(VERSION 2.8) 2 3 project(FlatBuffers) 4 5 # NOTE: Code coverage only works on Linux & OSX. 6 option(FLATBUFFERS_CODE_COVERAGE "Enable the code coverage build option." OFF) 7 option(FLATBUFFERS_BUILD_TESTS "Enable the build of tests and samples." ON) 8 option(FLATBUFFERS_INSTALL "Enable the installation of targets." ON) 9 option(FLATBUFFERS_BUILD_FLATLIB "Enable the build of the flatbuffers library" 10 ON) 11 option(FLATBUFFERS_BUILD_FLATC "Enable the build of the flatbuffers compiler" 12 ON) 13 option(FLATBUFFERS_BUILD_FLATHASH "Enable the build of flathash" ON) 14 option(FLATBUFFERS_BUILD_GRPCTEST "Enable the build of grpctest" OFF) 15 option(FLATBUFFERS_BUILD_SHAREDLIB 16 "Enable the build of the flatbuffers shared library" 17 OFF) 18 19 if(NOT FLATBUFFERS_BUILD_FLATC AND FLATBUFFERS_BUILD_TESTS) 20 message(WARNING 21 "Cannot build tests without building the compiler. Tests will be disabled.") 22 set(FLATBUFFERS_BUILD_TESTS OFF) 23 endif() 24 25 set(FlatBuffers_Library_SRCS 26 include/flatbuffers/code_generators.h 27 include/flatbuffers/flatbuffers.h 28 include/flatbuffers/hash.h 29 include/flatbuffers/idl.h 30 include/flatbuffers/util.h 31 include/flatbuffers/reflection.h 32 include/flatbuffers/reflection_generated.h 33 include/flatbuffers/flexbuffers.h 34 src/code_generators.cpp 35 src/idl_parser.cpp 36 src/idl_gen_text.cpp 37 src/reflection.cpp 38 src/util.cpp 39 ) 40 41 set(FlatBuffers_Compiler_SRCS 42 ${FlatBuffers_Library_SRCS} 43 src/idl_gen_cpp.cpp 44 src/idl_gen_general.cpp 45 src/idl_gen_go.cpp 46 src/idl_gen_js.cpp 47 src/idl_gen_php.cpp 48 src/idl_gen_python.cpp 49 src/idl_gen_fbs.cpp 50 src/idl_gen_grpc.cpp 51 src/flatc.cpp 52 src/flatc_main.cpp 53 grpc/src/compiler/schema_interface.h 54 grpc/src/compiler/cpp_generator.h 55 grpc/src/compiler/cpp_generator.cc 56 grpc/src/compiler/go_generator.h 57 grpc/src/compiler/go_generator.cc 58 ) 59 60 set(FlatHash_SRCS 61 include/flatbuffers/hash.h 62 src/flathash.cpp 63 ) 64 65 set(FlatBuffers_Tests_SRCS 66 ${FlatBuffers_Library_SRCS} 67 src/idl_gen_fbs.cpp 68 tests/test.cpp 69 # file generate by running compiler on tests/monster_test.fbs 70 ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h 71 ) 72 73 set(FlatBuffers_Sample_Binary_SRCS 74 include/flatbuffers/flatbuffers.h 75 samples/sample_binary.cpp 76 # file generated by running compiler on samples/monster.fbs 77 ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h 78 ) 79 80 set(FlatBuffers_Sample_Text_SRCS 81 ${FlatBuffers_Library_SRCS} 82 samples/sample_text.cpp 83 # file generated by running compiler on samples/monster.fbs 84 ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h 85 ) 86 87 set(FlatBuffers_GRPCTest_SRCS 88 include/flatbuffers/flatbuffers.h 89 include/flatbuffers/grpc.h 90 tests/monster_test.grpc.fb.h 91 tests/monster_test.grpc.fb.cc 92 grpc/tests/grpctest.cpp 93 # file generated by running compiler on samples/monster.fbs 94 ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h 95 ) 96 97 # source_group(Compiler FILES ${FlatBuffers_Compiler_SRCS}) 98 # source_group(Tests FILES ${FlatBuffers_Tests_SRCS}) 99 100 if(EXISTS "${CMAKE_TOOLCHAIN_FILE}") 101 # do not apply any global settings if the toolchain 102 # is being configured externally 103 elseif(APPLE) 104 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++") 105 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Werror -Wextra") 106 elseif(CMAKE_COMPILER_IS_GNUCXX) 107 if(CYGWIN) 108 set(CMAKE_CXX_FLAGS 109 "${CMAKE_CXX_FLAGS} -std=gnu++11") 110 else(CYGWIN) 111 set(CMAKE_CXX_FLAGS 112 "${CMAKE_CXX_FLAGS} -std=c++0x") 113 endif(CYGWIN) 114 set(CMAKE_CXX_FLAGS 115 "${CMAKE_CXX_FLAGS} -Wall -pedantic -Werror -Wextra -Werror=shadow") 116 if (GCC_VERSION VERSION_GREATER 4.4) 117 set(CMAKE_CXX_FLAGS 118 "${CMAKE_CXX_FLAGS} -Wunused-result -Werror=unused-result \ 119 -Wunused-parameter -Werror=unused-parameter") 120 endif() 121 122 # Certain platforms such as ARM do not use signed chars by default 123 # which causes issues with certain bounds checks. 124 set(CMAKE_CXX_FLAGS 125 "${CMAKE_CXX_FLAGS} -fsigned-char") 126 127 elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang") 128 set(CMAKE_CXX_FLAGS 129 "${CMAKE_CXX_FLAGS} -std=c++0x -stdlib=libc++ -Wall -pedantic -Werror \ 130 -Wextra") 131 if(NOT "${CMAKE_SYSTEM_NAME}" MATCHES "FreeBSD") 132 set(CMAKE_EXE_LINKER_FLAGS 133 "${CMAKE_EXE_LINKER_FLAGS} -lc++abi") 134 endif() 135 136 # Certain platforms such as ARM do not use signed chars by default 137 # which causes issues with certain bounds checks. 138 set(CMAKE_CXX_FLAGS 139 "${CMAKE_CXX_FLAGS} -fsigned-char") 140 141 endif() 142 143 if(FLATBUFFERS_CODE_COVERAGE) 144 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fprofile-arcs -ftest-coverage") 145 set(CMAKE_EXE_LINKER_FLAGS 146 "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage") 147 endif() 148 149 if(BIICODE) 150 include(biicode/cmake/biicode.cmake) 151 return() 152 endif() 153 154 include_directories(include) 155 include_directories(grpc) 156 157 if(FLATBUFFERS_BUILD_FLATLIB) 158 add_library(flatbuffers STATIC ${FlatBuffers_Library_SRCS}) 159 endif() 160 161 if(FLATBUFFERS_BUILD_FLATC) 162 add_executable(flatc ${FlatBuffers_Compiler_SRCS}) 163 if(NOT FLATBUFFERS_FLATC_EXECUTABLE) 164 set(FLATBUFFERS_FLATC_EXECUTABLE $<TARGET_FILE:flatc>) 165 endif() 166 endif() 167 168 if(FLATBUFFERS_BUILD_FLATHASH) 169 add_executable(flathash ${FlatHash_SRCS}) 170 endif() 171 172 if(FLATBUFFERS_BUILD_SHAREDLIB) 173 add_library(flatbuffers_shared SHARED ${FlatBuffers_Library_SRCS}) 174 set_target_properties(flatbuffers_shared PROPERTIES OUTPUT_NAME flatbuffers) 175 endif() 176 177 function(compile_flatbuffers_schema_to_cpp SRC_FBS) 178 get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH) 179 string(REGEX REPLACE "\\.fbs$" "_generated.h" GEN_HEADER ${SRC_FBS}) 180 add_custom_command( 181 OUTPUT ${GEN_HEADER} 182 COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}" -c --no-includes --gen-mutable 183 --gen-object-api -o "${SRC_FBS_DIR}" 184 "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}" 185 DEPENDS flatc) 186 endfunction() 187 188 function(compile_flatbuffers_schema_to_binary SRC_FBS) 189 get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH) 190 string(REGEX REPLACE "\\.fbs$" ".bfbs" GEN_BINARY_SCHEMA ${SRC_FBS}) 191 add_custom_command( 192 OUTPUT ${GEN_BINARY_SCHEMA} 193 COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}" -b --schema -o "${SRC_FBS_DIR}" 194 "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}" 195 DEPENDS flatc) 196 endfunction() 197 198 if(FLATBUFFERS_BUILD_TESTS) 199 compile_flatbuffers_schema_to_cpp(tests/monster_test.fbs) 200 include_directories(${CMAKE_CURRENT_BINARY_DIR}/tests) 201 add_executable(flattests ${FlatBuffers_Tests_SRCS}) 202 set_property(TARGET flattests 203 PROPERTY COMPILE_DEFINITIONS FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE 204 FLATBUFFERS_DEBUG_VERIFICATION_FAILURE=1) 205 206 compile_flatbuffers_schema_to_cpp(samples/monster.fbs) 207 include_directories(${CMAKE_CURRENT_BINARY_DIR}/samples) 208 add_executable(flatsamplebinary ${FlatBuffers_Sample_Binary_SRCS}) 209 add_executable(flatsampletext ${FlatBuffers_Sample_Text_SRCS}) 210 endif() 211 212 if(FLATBUFFERS_BUILD_GRPCTEST) 213 if(CMAKE_COMPILER_IS_GNUCXX) 214 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-parameter") 215 endif() 216 add_executable(grpctest ${FlatBuffers_GRPCTest_SRCS}) 217 target_link_libraries(grpctest grpc++_unsecure grpc pthread dl) 218 endif() 219 220 if(FLATBUFFERS_INSTALL) 221 install(DIRECTORY include/flatbuffers DESTINATION include) 222 if(FLATBUFFERS_BUILD_FLATLIB) 223 install(TARGETS flatbuffers DESTINATION lib) 224 endif() 225 if(FLATBUFFERS_BUILD_FLATC) 226 install(TARGETS flatc DESTINATION bin) 227 endif() 228 if(FLATBUFFERS_BUILD_SHAREDLIB) 229 install(TARGETS flatbuffers_shared DESTINATION lib) 230 endif() 231 endif() 232 233 if(FLATBUFFERS_BUILD_TESTS) 234 enable_testing() 235 236 file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/tests" DESTINATION 237 "${CMAKE_CURRENT_BINARY_DIR}") 238 add_test(NAME flattests COMMAND flattests) 239 endif() 240 241 include(CMake/BuildFlatBuffers.cmake) 242 243 if(FLATBUFFERS_PACKAGE_DEBIAN) 244 include(CMake/PackageDebian.cmake) 245 endif() 246