1cmake_minimum_required(VERSION 2.8) 2 3project(FlatBuffers) 4 5# NOTE: Code coverage only works on Linux & OSX. 6option(FLATBUFFERS_CODE_COVERAGE "Enable the code coverage build option." OFF) 7option(FLATBUFFERS_BUILD_TESTS "Enable the build of tests and samples." ON) 8option(FLATBUFFERS_INSTALL "Enable the installation of targets." ON) 9option(FLATBUFFERS_BUILD_FLATLIB "Enable the build of the flatbuffers library" 10 ON) 11option(FLATBUFFERS_BUILD_FLATC "Enable the build of the flatbuffers compiler" 12 ON) 13option(FLATBUFFERS_BUILD_FLATHASH "Enable the build of flathash" ON) 14option(FLATBUFFERS_BUILD_GRPCTEST "Enable the build of grpctest" OFF) 15option(FLATBUFFERS_BUILD_SHAREDLIB 16 "Enable the build of the flatbuffers shared library" 17 OFF) 18 19if(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) 23endif() 24 25set(FlatBuffers_Library_SRCS 26 include/flatbuffers/code_generators.h 27 include/flatbuffers/base.h 28 include/flatbuffers/flatbuffers.h 29 include/flatbuffers/hash.h 30 include/flatbuffers/idl.h 31 include/flatbuffers/util.h 32 include/flatbuffers/reflection.h 33 include/flatbuffers/reflection_generated.h 34 include/flatbuffers/stl_emulation.h 35 include/flatbuffers/flexbuffers.h 36 include/flatbuffers/registry.h 37 include/flatbuffers/minireflect.h 38 src/code_generators.cpp 39 src/idl_parser.cpp 40 src/idl_gen_text.cpp 41 src/reflection.cpp 42 src/util.cpp 43) 44 45set(FlatBuffers_Compiler_SRCS 46 ${FlatBuffers_Library_SRCS} 47 src/idl_gen_cpp.cpp 48 src/idl_gen_general.cpp 49 src/idl_gen_go.cpp 50 src/idl_gen_js.cpp 51 src/idl_gen_php.cpp 52 src/idl_gen_python.cpp 53 src/idl_gen_fbs.cpp 54 src/idl_gen_grpc.cpp 55 src/idl_gen_json_schema.cpp 56 src/flatc.cpp 57 src/flatc_main.cpp 58 grpc/src/compiler/schema_interface.h 59 grpc/src/compiler/cpp_generator.h 60 grpc/src/compiler/cpp_generator.cc 61 grpc/src/compiler/go_generator.h 62 grpc/src/compiler/go_generator.cc 63) 64 65set(FlatHash_SRCS 66 include/flatbuffers/hash.h 67 src/flathash.cpp 68) 69 70set(FlatBuffers_Tests_SRCS 71 ${FlatBuffers_Library_SRCS} 72 src/idl_gen_fbs.cpp 73 tests/test.cpp 74 # file generate by running compiler on tests/monster_test.fbs 75 ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h 76) 77 78set(FlatBuffers_Sample_Binary_SRCS 79 include/flatbuffers/flatbuffers.h 80 samples/sample_binary.cpp 81 # file generated by running compiler on samples/monster.fbs 82 ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h 83) 84 85set(FlatBuffers_Sample_Text_SRCS 86 ${FlatBuffers_Library_SRCS} 87 samples/sample_text.cpp 88 # file generated by running compiler on samples/monster.fbs 89 ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h 90) 91 92set(FlatBuffers_GRPCTest_SRCS 93 include/flatbuffers/flatbuffers.h 94 include/flatbuffers/grpc.h 95 tests/monster_test.grpc.fb.h 96 tests/monster_test.grpc.fb.cc 97 grpc/tests/grpctest.cpp 98 # file generated by running compiler on samples/monster.fbs 99 ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h 100) 101 102# source_group(Compiler FILES ${FlatBuffers_Compiler_SRCS}) 103# source_group(Tests FILES ${FlatBuffers_Tests_SRCS}) 104 105if(EXISTS "${CMAKE_TOOLCHAIN_FILE}") 106 # do not apply any global settings if the toolchain 107 # is being configured externally 108elseif(APPLE) 109 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++") 110 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Werror -Wextra -Wno-unused-parameter") 111elseif(CMAKE_COMPILER_IS_GNUCXX) 112 if(CYGWIN) 113 set(CMAKE_CXX_FLAGS 114 "${CMAKE_CXX_FLAGS} -std=gnu++11") 115 else(CYGWIN) 116 set(CMAKE_CXX_FLAGS 117 "${CMAKE_CXX_FLAGS} -std=c++0x") 118 endif(CYGWIN) 119 set(CMAKE_CXX_FLAGS 120 "${CMAKE_CXX_FLAGS} -Wall -pedantic -Werror -Wextra -Werror=shadow") 121 if (GCC_VERSION VERSION_GREATER 4.4) 122 set(CMAKE_CXX_FLAGS 123 "${CMAKE_CXX_FLAGS} -Wunused-result -Werror=unused-result \ 124 -Wunused-parameter -Werror=unused-parameter") 125 endif() 126 127 # Certain platforms such as ARM do not use signed chars by default 128 # which causes issues with certain bounds checks. 129 set(CMAKE_CXX_FLAGS 130 "${CMAKE_CXX_FLAGS} -fsigned-char") 131 132elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang") 133 set(CMAKE_CXX_FLAGS 134 "${CMAKE_CXX_FLAGS} -std=c++0x -Wall -pedantic -Werror -Wextra -Wno-unused-parameter") 135 if(NOT "${CMAKE_SYSTEM_NAME}" MATCHES "Linux") 136 set(CMAKE_CXX_FLAGS 137 "${CMAKE_CXX_FLAGS} -stdlib=libc++") 138 endif() 139 if(NOT ("${CMAKE_SYSTEM_NAME}" MATCHES "FreeBSD" OR 140 "${CMAKE_SYSTEM_NAME}" MATCHES "Linux")) 141 set(CMAKE_EXE_LINKER_FLAGS 142 "${CMAKE_EXE_LINKER_FLAGS} -lc++abi") 143 endif() 144 145 # Certain platforms such as ARM do not use signed chars by default 146 # which causes issues with certain bounds checks. 147 set(CMAKE_CXX_FLAGS 148 "${CMAKE_CXX_FLAGS} -fsigned-char") 149 150elseif(MSVC) 151 # Visual Studio pedantic build settings 152 # warning C4512: assignment operator could not be generated 153 # warning C4316: object allocated on the heap may not be aligned 154 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4 /WX /wd4512 /wd4316") 155endif() 156 157if(FLATBUFFERS_CODE_COVERAGE) 158 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fprofile-arcs -ftest-coverage") 159 set(CMAKE_EXE_LINKER_FLAGS 160 "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage") 161endif() 162 163if(BIICODE) 164 include(biicode/cmake/biicode.cmake) 165 return() 166endif() 167 168include_directories(include) 169include_directories(grpc) 170 171if(FLATBUFFERS_BUILD_FLATLIB) 172add_library(flatbuffers STATIC ${FlatBuffers_Library_SRCS}) 173endif() 174 175if(FLATBUFFERS_BUILD_FLATC) 176 add_executable(flatc ${FlatBuffers_Compiler_SRCS}) 177 if(NOT FLATBUFFERS_FLATC_EXECUTABLE) 178 set(FLATBUFFERS_FLATC_EXECUTABLE $<TARGET_FILE:flatc>) 179 endif() 180 if(MSVC) 181 # Make flatc.exe not depend on runtime dlls for easy distribution. 182 target_compile_options(flatc PUBLIC $<$<CONFIG:Release>:/MT>) 183 endif() 184endif() 185 186if(FLATBUFFERS_BUILD_FLATHASH) 187 add_executable(flathash ${FlatHash_SRCS}) 188endif() 189 190if(FLATBUFFERS_BUILD_SHAREDLIB) 191 add_library(flatbuffers_shared SHARED ${FlatBuffers_Library_SRCS}) 192 193 # Shared object version: "major.minor.micro" 194 # - micro updated every release when there is no API/ABI changes 195 # - minor updated when there are additions in API/ABI 196 # - major (ABI number) updated when there are changes in ABI (or removals) 197 set(FlatBuffers_Library_SONAME_MAJOR "1") 198 set(FlatBuffers_Library_SONAME_FULL "${FlatBuffers_Library_SONAME_MAJOR}.8.0") 199 set_target_properties(flatbuffers_shared PROPERTIES OUTPUT_NAME flatbuffers 200 SOVERSION "${FlatBuffers_Library_SONAME_MAJOR}" 201 VERSION "${FlatBuffers_Library_SONAME_FULL}") 202endif() 203 204function(compile_flatbuffers_schema_to_cpp SRC_FBS) 205 get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH) 206 string(REGEX REPLACE "\\.fbs$" "_generated.h" GEN_HEADER ${SRC_FBS}) 207 add_custom_command( 208 OUTPUT ${GEN_HEADER} 209 COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}" -c --no-includes --gen-mutable 210 --gen-object-api -o "${SRC_FBS_DIR}" 211 --cpp-ptr-type flatbuffers::unique_ptr # Used to test with C++98 STLs 212 --reflect-names 213 -I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test" 214 "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}" 215 DEPENDS flatc) 216endfunction() 217 218function(compile_flatbuffers_schema_to_binary SRC_FBS) 219 get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH) 220 string(REGEX REPLACE "\\.fbs$" ".bfbs" GEN_BINARY_SCHEMA ${SRC_FBS}) 221 add_custom_command( 222 OUTPUT ${GEN_BINARY_SCHEMA} 223 COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}" -b --schema -o "${SRC_FBS_DIR}" 224 "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}" 225 DEPENDS flatc) 226endfunction() 227 228if(FLATBUFFERS_BUILD_TESTS) 229 compile_flatbuffers_schema_to_cpp(tests/monster_test.fbs) 230 include_directories(${CMAKE_CURRENT_BINARY_DIR}/tests) 231 add_executable(flattests ${FlatBuffers_Tests_SRCS}) 232 set_property(TARGET flattests 233 PROPERTY COMPILE_DEFINITIONS FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE 234 FLATBUFFERS_DEBUG_VERIFICATION_FAILURE=1) 235 236 compile_flatbuffers_schema_to_cpp(samples/monster.fbs) 237 include_directories(${CMAKE_CURRENT_BINARY_DIR}/samples) 238 add_executable(flatsamplebinary ${FlatBuffers_Sample_Binary_SRCS}) 239 add_executable(flatsampletext ${FlatBuffers_Sample_Text_SRCS}) 240endif() 241 242if(FLATBUFFERS_BUILD_GRPCTEST) 243 if(CMAKE_COMPILER_IS_GNUCXX) 244 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-parameter -Wno-shadow") 245 endif() 246 add_executable(grpctest ${FlatBuffers_GRPCTest_SRCS}) 247 target_link_libraries(grpctest grpc++_unsecure grpc_unsecure pthread dl) 248endif() 249 250if(FLATBUFFERS_INSTALL) 251 include(GNUInstallDirs) 252 253 install(DIRECTORY include/flatbuffers DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) 254 255 set(FB_CMAKE_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/flatbuffers") 256 257 install( 258 FILES "CMake/FlatbuffersConfig.cmake" 259 DESTINATION ${FB_CMAKE_DIR} 260 ) 261 262 if(FLATBUFFERS_BUILD_FLATLIB) 263 if(CMAKE_VERSION VERSION_LESS 3.0) 264 install( 265 TARGETS flatbuffers EXPORT FlatbuffersTargets 266 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} 267 ) 268 else() 269 install( 270 TARGETS flatbuffers EXPORT FlatbuffersTargets 271 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} 272 INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} 273 ) 274 endif() 275 276 install(EXPORT FlatbuffersTargets 277 FILE FlatbuffersTargets.cmake 278 NAMESPACE flatbuffers:: 279 DESTINATION ${FB_CMAKE_DIR} 280 ) 281 endif() 282 283 if(FLATBUFFERS_BUILD_FLATC) 284 install( 285 TARGETS flatc EXPORT FlatcTargets 286 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} 287 CONFIGURATIONS Release 288 ) 289 290 install( 291 EXPORT FlatcTargets 292 FILE FlatcTargets.cmake 293 NAMESPACE flatbuffers:: 294 DESTINATION ${FB_CMAKE_DIR} 295 CONFIGURATIONS Release 296 ) 297 endif() 298 299 if(FLATBUFFERS_BUILD_SHAREDLIB) 300 if(CMAKE_VERSION VERSION_LESS 3.0) 301 install( 302 TARGETS flatbuffers_shared EXPORT FlatbuffersSharedTargets 303 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} 304 RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR} 305 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} 306 ) 307 else() 308 install( 309 TARGETS flatbuffers_shared EXPORT FlatbuffersSharedTargets 310 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} 311 RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR} 312 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} 313 INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} 314 ) 315 endif() 316 317 install( 318 EXPORT FlatbuffersSharedTargets 319 FILE FlatbuffersSharedTargets.cmake 320 NAMESPACE flatbuffers:: 321 DESTINATION ${FB_CMAKE_DIR} 322 ) 323 endif() 324endif() 325 326if(FLATBUFFERS_BUILD_TESTS) 327 enable_testing() 328 329 file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/tests" DESTINATION 330 "${CMAKE_CURRENT_BINARY_DIR}") 331 add_test(NAME flattests COMMAND flattests) 332endif() 333 334include(CMake/BuildFlatBuffers.cmake) 335 336if(FLATBUFFERS_PACKAGE_DEBIAN) 337 include(CMake/PackageDebian.cmake) 338endif() 339