1# This is the legacy minimum version flatbuffers supported for a while. 2cmake_minimum_required(VERSION 2.8.12...3.22.1) 3 4# CMake version 3.16 is the 'de-facto' minimum version for flatbuffers. If the 5# current cmake is older than this, warn the user and include the legacy file to 6# provide some level of support. 7if(CMAKE_VERSION VERSION_LESS 3.16) 8 message(WARNING "Using cmake version ${CMAKE_VERSION} which is older than " 9 "our target version of 3.16. This will use the legacy CMakeLists.txt that " 10 "supports version 2.8.12 and higher, but not actively maintained. Consider " 11 "upgrading cmake to a newer version, as this may become a fatal error in the " 12 "future.") 13 # Use the legacy version of CMakeLists.txt 14 include(CMake/CMakeLists_legacy.cmake.in) 15 return() 16endif() 17 18# Attempt to read the current version of flatbuffers by looking at the latest tag. 19include(CMake/Version.cmake) 20 21if (POLICY CMP0048) 22 cmake_policy(SET CMP0048 NEW) 23 project(FlatBuffers 24 DESCRIPTION "Flatbuffers serialization library" 25 VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH} 26 LANGUAGES CXX) 27else() 28 project(FlatBuffers) 29endif (POLICY CMP0048) 30 31# generate compile_commands.json 32set(CMAKE_EXPORT_COMPILE_COMMANDS ON) 33 34# NOTE: Code coverage only works on Linux & OSX. 35option(FLATBUFFERS_CODE_COVERAGE "Enable the code coverage build option." OFF) 36option(FLATBUFFERS_BUILD_TESTS "Enable the build of tests and samples." ON) 37option(FLATBUFFERS_INSTALL "Enable the installation of targets." ON) 38option(FLATBUFFERS_BUILD_FLATLIB "Enable the build of the flatbuffers library" 39 ON) 40option(FLATBUFFERS_BUILD_FLATC "Enable the build of the flatbuffers compiler" 41 ON) 42option(FLATBUFFERS_STATIC_FLATC "Build flatbuffers compiler with -static flag" 43 OFF) 44option(FLATBUFFERS_BUILD_FLATHASH "Enable the build of flathash" ON) 45option(FLATBUFFERS_BUILD_BENCHMARKS "Enable the build of flatbenchmark." 46 OFF) 47option(FLATBUFFERS_BUILD_GRPCTEST "Enable the build of grpctest" OFF) 48option(FLATBUFFERS_BUILD_SHAREDLIB 49 "Enable the build of the flatbuffers shared library" 50 OFF) 51option(FLATBUFFERS_LIBCXX_WITH_CLANG "Force libc++ when using Clang" ON) 52# NOTE: Sanitizer check only works on Linux & OSX (gcc & llvm). 53option(FLATBUFFERS_CODE_SANITIZE 54 "Add '-fsanitize' flags to 'flattests' and 'flatc' targets." 55 OFF) 56option(FLATBUFFERS_PACKAGE_REDHAT 57 "Build an rpm using the 'package' target." 58 OFF) 59option(FLATBUFFERS_PACKAGE_DEBIAN 60 "Build an deb using the 'package' target." 61 OFF) 62option(FLATBUFFERS_BUILD_CPP17 63 "Enable the build of c++17 test target. \" 64 Requirements: Clang6, GCC7, MSVC2017 (_MSC_VER >= 1914) or higher." 65 OFF) 66option(FLATBUFFERS_BUILD_LEGACY 67 "Run C++ code generator with '--cpp-std c++0x' switch." 68 OFF) 69option(FLATBUFFERS_ENABLE_PCH 70 "Enable precompile headers support for 'flatbuffers' and 'flatc'. \" 71 Only work if CMake supports 'target_precompile_headers'. \" 72 This can speed up compilation time." 73 OFF) 74option(FLATBUFFERS_SKIP_MONSTER_EXTRA 75 "Skip generating monster_extra.fbs that contains non-supported numerical\" 76 types." OFF) 77option(FLATBUFFERS_STRICT_MODE 78 "Build flatbuffers with all warnings as errors (-Werror or /WX)." 79 OFF) 80 81set(MSVC_LIKE OFF) 82if(MSVC OR CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC") 83 set(MSVC_LIKE ON) 84endif() 85 86if(NOT FLATBUFFERS_BUILD_FLATC AND FLATBUFFERS_BUILD_TESTS) 87 message(WARNING 88 "Cannot build tests without building the compiler. Tests will be disabled.") 89 set(FLATBUFFERS_BUILD_TESTS OFF) 90endif() 91 92if(DEFINED FLATBUFFERS_MAX_PARSING_DEPTH) 93 # Override the default recursion depth limit. 94 add_definitions(-DFLATBUFFERS_MAX_PARSING_DEPTH=${FLATBUFFERS_MAX_PARSING_DEPTH}) 95 message(STATUS "FLATBUFFERS_MAX_PARSING_DEPTH: ${FLATBUFFERS_MAX_PARSING_DEPTH}") 96endif() 97 98# Auto-detect locale-narrow 'strtod_l' and 'strtoull_l' functions. 99if(NOT DEFINED FLATBUFFERS_LOCALE_INDEPENDENT) 100 include(CheckCXXSymbolExists) 101 102 set(FLATBUFFERS_LOCALE_INDEPENDENT 0) 103 if(MSVC_LIKE) 104 check_cxx_symbol_exists(_strtof_l stdlib.h FLATBUFFERS_HAS_STRTOF_L) 105 check_cxx_symbol_exists(_strtoui64_l stdlib.h FLATBUFFERS_HAS_STRTOULL_L) 106 else() 107 check_cxx_symbol_exists(strtof_l stdlib.h FLATBUFFERS_HAS_STRTOF_L) 108 check_cxx_symbol_exists(strtoull_l stdlib.h FLATBUFFERS_HAS_STRTOULL_L) 109 endif() 110 if(FLATBUFFERS_HAS_STRTOF_L AND FLATBUFFERS_HAS_STRTOULL_L) 111 set(FLATBUFFERS_LOCALE_INDEPENDENT 1) 112 endif() 113endif() 114add_definitions(-DFLATBUFFERS_LOCALE_INDEPENDENT=$<BOOL:${FLATBUFFERS_LOCALE_INDEPENDENT}>) 115 116set(FlatBuffers_Library_SRCS 117 include/flatbuffers/allocator.h 118 include/flatbuffers/array.h 119 include/flatbuffers/base.h 120 include/flatbuffers/bfbs_generator.h 121 include/flatbuffers/buffer.h 122 include/flatbuffers/buffer_ref.h 123 include/flatbuffers/default_allocator.h 124 include/flatbuffers/detached_buffer.h 125 include/flatbuffers/flatbuffer_builder.h 126 include/flatbuffers/flatbuffers.h 127 include/flatbuffers/flexbuffers.h 128 include/flatbuffers/flex_flat_util.h 129 include/flatbuffers/hash.h 130 include/flatbuffers/idl.h 131 include/flatbuffers/minireflect.h 132 include/flatbuffers/reflection.h 133 include/flatbuffers/reflection_generated.h 134 include/flatbuffers/registry.h 135 include/flatbuffers/stl_emulation.h 136 include/flatbuffers/string.h 137 include/flatbuffers/struct.h 138 include/flatbuffers/table.h 139 include/flatbuffers/util.h 140 include/flatbuffers/vector.h 141 include/flatbuffers/vector_downward.h 142 include/flatbuffers/verifier.h 143 src/idl_parser.cpp 144 src/idl_gen_text.cpp 145 src/reflection.cpp 146 src/util.cpp 147) 148 149set(FlatBuffers_Compiler_SRCS 150 ${FlatBuffers_Library_SRCS} 151 src/idl_gen_cpp.cpp 152 src/idl_gen_csharp.cpp 153 src/idl_gen_dart.cpp 154 src/idl_gen_kotlin.cpp 155 src/idl_gen_go.cpp 156 src/idl_gen_java.cpp 157 src/idl_gen_ts.cpp 158 src/idl_gen_php.cpp 159 src/idl_gen_python.cpp 160 src/idl_gen_lobster.cpp 161 src/idl_gen_lua.cpp 162 src/idl_gen_rust.cpp 163 src/idl_gen_fbs.cpp 164 src/idl_gen_grpc.cpp 165 src/idl_gen_json_schema.cpp 166 src/idl_gen_swift.cpp 167 src/idl_namer.h 168 src/namer.h 169 src/flatc.cpp 170 src/flatc_main.cpp 171 src/bfbs_gen.h 172 src/bfbs_gen_lua.h 173 src/bfbs_namer.h 174 include/flatbuffers/code_generators.h 175 src/binary_annotator.h 176 src/binary_annotator.cpp 177 src/annotated_binary_text_gen.h 178 src/annotated_binary_text_gen.cpp 179 src/bfbs_gen_lua.cpp 180 src/code_generators.cpp 181 grpc/src/compiler/schema_interface.h 182 grpc/src/compiler/cpp_generator.h 183 grpc/src/compiler/cpp_generator.cc 184 grpc/src/compiler/go_generator.h 185 grpc/src/compiler/go_generator.cc 186 grpc/src/compiler/java_generator.h 187 grpc/src/compiler/java_generator.cc 188 grpc/src/compiler/python_generator.h 189 grpc/src/compiler/python_generator.cc 190 grpc/src/compiler/swift_generator.h 191 grpc/src/compiler/swift_generator.cc 192 grpc/src/compiler/ts_generator.h 193 grpc/src/compiler/ts_generator.cc 194) 195 196set(FlatHash_SRCS 197 include/flatbuffers/hash.h 198 src/flathash.cpp 199) 200 201set(FlatBuffers_Tests_SRCS 202 ${FlatBuffers_Library_SRCS} 203 src/idl_gen_fbs.cpp 204 tests/test.cpp 205 tests/test_assert.h 206 tests/test_assert.cpp 207 tests/test_builder.h 208 tests/test_builder.cpp 209 tests/native_type_test_impl.h 210 tests/native_type_test_impl.cpp 211 include/flatbuffers/code_generators.h 212 src/code_generators.cpp 213 # file generate by running compiler on tests/monster_test.fbs 214 ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h 215 # file generate by running compiler on namespace_test/namespace_test1.fbs 216 ${CMAKE_CURRENT_BINARY_DIR}/tests/namespace_test/namespace_test1_generated.h 217 ${CMAKE_CURRENT_BINARY_DIR}/tests/namespace_test/namespace_test2_generated.h 218 # file generate by running compiler on union_vector/union_vector.fbs 219 ${CMAKE_CURRENT_BINARY_DIR}/tests/union_vector/union_vector_generated.h 220 # file generate by running compiler on tests/arrays_test.fbs 221 ${CMAKE_CURRENT_BINARY_DIR}/tests/arrays_test_generated.h 222 # file generate by running compiler on tests/native_type_test.fbs 223 ${CMAKE_CURRENT_BINARY_DIR}/tests/native_type_test_generated.h 224 # file generate by running compiler on tests/monster_extra.fbs 225 ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_extra_generated.h 226 # file generate by running compiler on tests/monster_test.fbs 227 ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_bfbs_generated.h 228 # file generate by running compiler on tests/optional_scalars.fbs 229 ${CMAKE_CURRENT_BINARY_DIR}/tests/optional_scalars_generated.h 230) 231 232set(FlatBuffers_Tests_CPP17_SRCS 233 ${FlatBuffers_Library_SRCS} 234 tests/test_assert.h 235 tests/test_assert.cpp 236 tests/cpp17/test_cpp17.cpp 237 # file generate by running compiler on tests/monster_test.fbs 238 ${CMAKE_CURRENT_BINARY_DIR}/tests/cpp17/generated_cpp17/monster_test_generated.h 239 ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h 240 ${CMAKE_CURRENT_BINARY_DIR}/tests/cpp17/generated_cpp17/optional_scalars_generated.h 241 ${CMAKE_CURRENT_BINARY_DIR}/tests/optional_scalars_generated.h 242) 243 244set(FlatBuffers_Sample_Binary_SRCS 245 include/flatbuffers/flatbuffers.h 246 samples/sample_binary.cpp 247 # file generated by running compiler on samples/monster.fbs 248 ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h 249) 250 251set(FlatBuffers_Sample_Text_SRCS 252 ${FlatBuffers_Library_SRCS} 253 samples/sample_text.cpp 254 # file generated by running compiler on samples/monster.fbs 255 ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h 256) 257 258set(FlatBuffers_Sample_BFBS_SRCS 259 ${FlatBuffers_Library_SRCS} 260 samples/sample_bfbs.cpp 261 # file generated by running compiler on samples/monster.fbs 262 ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h 263) 264 265set(FlatBuffers_GRPCTest_SRCS 266 include/flatbuffers/flatbuffers.h 267 include/flatbuffers/grpc.h 268 include/flatbuffers/util.h 269 src/util.cpp 270 tests/monster_test.grpc.fb.h 271 tests/test_assert.h 272 tests/test_builder.h 273 tests/monster_test.grpc.fb.cc 274 tests/test_assert.cpp 275 tests/test_builder.cpp 276 grpc/tests/grpctest.cpp 277 grpc/tests/message_builder_test.cpp 278 # file generate by running compiler on tests/monster_test.fbs 279 ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h 280) 281 282# TODO(dbaileychess): Figure out how this would now work. I posted a question on 283# https://stackoverflow.com/questions/71772330/override-target-compile-options-via-cmake-command-line. 284# Append FLATBUFFERS_CXX_FLAGS to CMAKE_CXX_FLAGS. 285if(DEFINED FLATBUFFERS_CXX_FLAGS AND NOT EXISTS "${CMAKE_TOOLCHAIN_FILE}") 286 message(STATUS "extend CXX_FLAGS with ${FLATBUFFERS_CXX_FLAGS}") 287 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FLATBUFFERS_CXX_FLAGS}") 288endif() 289message(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}") 290 291function(add_fsanitize_to_target _target _sanitizer) 292 if(WIN32) 293 target_compile_definitions(${_target} PRIVATE FLATBUFFERS_MEMORY_LEAK_TRACKING) 294 message(STATUS "Sanitizer MSVC::_CrtDumpMemoryLeaks added to ${_target}") 295 else() 296 # FLATBUFFERS_CODE_SANITIZE: boolean {ON,OFF,YES,NO} or string with list of sanitizer. 297 # List of sanitizer is string starts with '=': "=address,undefined,thread,memory". 298 if((${CMAKE_CXX_COMPILER_ID} MATCHES "Clang") OR 299 ((${CMAKE_CXX_COMPILER_ID} MATCHES "GNU") AND NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.9")) 300 ) 301 set(_sanitizer_flags "=address,undefined") 302 if(_sanitizer MATCHES "=.*") 303 # override default by user-defined sanitizer list 304 set(_sanitizer_flags ${_sanitizer}) 305 endif() 306 target_compile_options(${_target} PRIVATE 307 -g -fsigned-char -fno-omit-frame-pointer 308 "-fsanitize${_sanitizer_flags}") 309 target_link_libraries(${_target} PRIVATE 310 "-fsanitize${_sanitizer_flags}") 311 set_property(TARGET ${_target} PROPERTY POSITION_INDEPENDENT_CODE ON) 312 message(STATUS "Sanitizer ${_sanitizer_flags} added to ${_target}") 313 endif() 314 endif() 315endfunction() 316 317function(add_pch_to_target _target _pch_header) 318 if(COMMAND target_precompile_headers) 319 target_precompile_headers(${_target} PRIVATE ${_pch_header}) 320 if(NOT MSVC) 321 set_source_files_properties(src/util.cpp PROPERTIES SKIP_PRECOMPILE_HEADERS ON) 322 endif() 323 endif() 324endfunction() 325 326include_directories(include) 327include_directories(grpc) 328 329# Creates an interface library that stores the configuration settings that each 330# target links too. This is a compromise between setting configuration globally 331# with add_compile_options() and the more targetted target_compile_options(). 332# This way each target in this file can share settings and override them if 333# needed. 334add_library(ProjectConfig INTERFACE) 335target_compile_features(ProjectConfig 336 INTERFACE 337 cxx_std_11 338) 339 340# Force the standard to be met. 341set(CMAKE_CXX_STANDARD_REQUIRED ON) 342 343# We shouldn't rely on any compiler-extensions to make things work. 344set(CMAKE_CXX_EXTENSIONS OFF) 345 346if(MSVC_LIKE) 347 target_compile_options(ProjectConfig 348 INTERFACE 349 /W4 350 $<$<BOOL:${FLATBUFFERS_STRICT_MODE}>: 351 /WX # Treat all compiler warnings as errors 352 > 353 /wd4512 # C4512: assignment operator could not be generated 354 /wd4316 # C4316: object allocated on the heap may not be aligned 355 /wd4456 # C4456: hides previous local declaration 356 $<$<CXX_COMPILER_ID:Clang>: 357 /D_CRT_SECURE_NO_WARNINGS 358 > 359 ) 360else() 361 target_compile_options(ProjectConfig 362 INTERFACE 363 -Wall 364 -Wno-unknown-warning-option 365 $<$<BOOL:${FLATBUFFERS_STRICT_MODE}>: 366 -Werror # Treat all compiler warnings as errors 367 368 # False positive string overflow 369 # https://github.com/google/flatbuffers/issues/7366 370 -Wno-error=stringop-overflow 371 > 372 -pedantic 373 -Wextra 374 -Wno-unused-parameter 375 -Wold-style-cast 376 -Wimplicit-fallthrough 377 -fsigned-char 378 -Wnon-virtual-dtor 379 380 $<$<CXX_COMPILER_ID:CLANG>: 381 -Wmissing-declarations 382 -Wzero-as-null-pointer-constant 383 $<$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,3.8>: 384 -Wimplicit-fallthrough 385 -Wextra-semi 386 $<$<BOOL:${FLATBUFFERS_STRICT_MODE}>: 387 -Werror=unused-private-field 388 > 389 > 390 > 391 392 $<$<CXX_COMPILER_ID:GNU>: 393 $<$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,4.4>: 394 -Wunused-result 395 -Wunused-parameter 396 -Werror=unused-parameter 397 -Wmissing-declarations 398 > 399 $<$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,4.7>: 400 -Wzero-as-null-pointer-constant 401 > 402 $<$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,7.0>: 403 -faligned-new 404 $<$<BOOL:${FLATBUFFERS_STRICT_MODE}>: 405 -Werror=implicit-fallthrough=2 406 > 407 > 408 $<$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,8.0>: 409 -Wextra-semi 410 > 411 > 412 413 $<$<BOOL:${FLATBUFFERS_CODE_COVERAGE}>: 414 -g 415 -fprofile-arcs 416 -ftest-coverage 417 > 418 ) 419 420 if(FLATBUFFERS_CODE_COVERAGE) 421 target_link_options(ProjectConfig 422 INTERFACE 423 -fprofile-arcs 424 -ftest-coverage 425 ) 426 endif() 427endif() 428 429if(FLATBUFFERS_BUILD_FLATLIB) 430 add_library(flatbuffers STATIC ${FlatBuffers_Library_SRCS}) 431 432 # Attach header directory for when build via add_subdirectory(). 433 target_include_directories(flatbuffers 434 INTERFACE 435 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> 436 ) 437 target_link_libraries(flatbuffers PRIVATE $<BUILD_INTERFACE:ProjectConfig>) 438 439 if(FLATBUFFERS_ENABLE_PCH) 440 add_pch_to_target(flatbuffers include/flatbuffers/pch/pch.h) 441 endif() 442endif() 443 444if(FLATBUFFERS_BUILD_FLATC) 445 add_executable(flatc ${FlatBuffers_Compiler_SRCS}) 446 if(FLATBUFFERS_ENABLE_PCH) 447 add_pch_to_target(flatc include/flatbuffers/pch/flatc_pch.h) 448 endif() 449 450 target_link_libraries(flatc PRIVATE $<BUILD_INTERFACE:ProjectConfig>) 451 target_compile_options(flatc 452 PUBLIC 453 $<$<AND:$<BOOL:${MSVC_LIKE}>,$<CONFIG:Release>>: 454 /MT 455 > 456 ) 457 458 if(FLATBUFFERS_CODE_SANITIZE AND NOT WIN32) 459 add_fsanitize_to_target(flatc ${FLATBUFFERS_CODE_SANITIZE}) 460 endif() 461 if(NOT FLATBUFFERS_FLATC_EXECUTABLE) 462 set(FLATBUFFERS_FLATC_EXECUTABLE $<TARGET_FILE:flatc>) 463 endif() 464 if(FLATBUFFERS_STATIC_FLATC AND NOT MSVC) 465 target_link_libraries(flatc PRIVATE -static) 466 endif() 467endif() 468 469if(FLATBUFFERS_BUILD_FLATHASH) 470 add_executable(flathash ${FlatHash_SRCS}) 471 target_link_libraries(flathash PRIVATE $<BUILD_INTERFACE:ProjectConfig>) 472endif() 473 474if(FLATBUFFERS_BUILD_SHAREDLIB) 475 add_library(flatbuffers_shared SHARED ${FlatBuffers_Library_SRCS}) 476 target_link_libraries(flatbuffers_shared PRIVATE $<BUILD_INTERFACE:ProjectConfig>) 477 # Shared object version: "major.minor.micro" 478 # - micro updated every release when there is no API/ABI changes 479 # - minor updated when there are additions in API/ABI 480 # - major (ABI number) updated when there are changes in ABI (or removals) 481 set(FlatBuffers_Library_SONAME_MAJOR ${VERSION_MAJOR}) 482 set(FlatBuffers_Library_SONAME_FULL "${FlatBuffers_Library_SONAME_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}") 483 set_target_properties(flatbuffers_shared PROPERTIES OUTPUT_NAME flatbuffers 484 SOVERSION "${FlatBuffers_Library_SONAME_MAJOR}" 485 VERSION "${FlatBuffers_Library_SONAME_FULL}") 486 if(FLATBUFFERS_ENABLE_PCH) 487 add_pch_to_target(flatbuffers_shared include/flatbuffers/pch/pch.h) 488 endif() 489endif() 490 491# Global list of generated files. 492# Use the global property to be independent of PARENT_SCOPE. 493set_property(GLOBAL PROPERTY FBS_GENERATED_OUTPUTS) 494 495function(get_generated_output generated_files) 496 get_property(tmp GLOBAL PROPERTY FBS_GENERATED_OUTPUTS) 497 set(${generated_files} ${tmp} PARENT_SCOPE) 498endfunction(get_generated_output) 499 500function(register_generated_output file_name) 501 get_property(tmp GLOBAL PROPERTY FBS_GENERATED_OUTPUTS) 502 list(APPEND tmp ${file_name}) 503 set_property(GLOBAL PROPERTY FBS_GENERATED_OUTPUTS ${tmp}) 504endfunction(register_generated_output) 505 506function(compile_flatbuffers_schema_to_cpp_opt SRC_FBS OPT) 507 if(FLATBUFFERS_BUILD_LEGACY) 508 set(OPT ${OPT};--cpp-std c++0x) 509 else() 510 # --cpp-std is defined by flatc default settings. 511 endif() 512 message(STATUS "`${SRC_FBS}`: add generation of C++ code with '${OPT}'") 513 get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH) 514 string(REGEX REPLACE "\\.fbs$" "_generated.h" GEN_HEADER ${SRC_FBS}) 515 add_custom_command( 516 OUTPUT ${GEN_HEADER} 517 COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}" 518 --cpp --gen-mutable --gen-object-api --reflect-names 519 --cpp-ptr-type flatbuffers::unique_ptr # Used to test with C++98 STLs 520 ${OPT} 521 -I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test" 522 -o "${SRC_FBS_DIR}" 523 "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}" 524 DEPENDS flatc 525 COMMENT "Run generation: '${GEN_HEADER}'") 526 register_generated_output(${GEN_HEADER}) 527endfunction() 528 529function(compile_flatbuffers_schema_to_cpp SRC_FBS) 530 compile_flatbuffers_schema_to_cpp_opt(${SRC_FBS} "--no-includes;--gen-compare") 531endfunction() 532 533function(compile_flatbuffers_schema_to_binary SRC_FBS) 534 message(STATUS "`${SRC_FBS}`: add generation of binary (.bfbs) schema") 535 get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH) 536 string(REGEX REPLACE "\\.fbs$" ".bfbs" GEN_BINARY_SCHEMA ${SRC_FBS}) 537 # For details about flags see generate_code.py 538 add_custom_command( 539 OUTPUT ${GEN_BINARY_SCHEMA} 540 COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}" 541 -b --schema --bfbs-comments --bfbs-builtins 542 --bfbs-filenames ${SRC_FBS_DIR} 543 -I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test" 544 -o "${SRC_FBS_DIR}" 545 "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}" 546 DEPENDS flatc 547 COMMENT "Run generation: '${GEN_BINARY_SCHEMA}'") 548 register_generated_output(${GEN_BINARY_SCHEMA}) 549endfunction() 550 551function(compile_flatbuffers_schema_to_embedded_binary SRC_FBS OPT) 552 if(FLATBUFFERS_BUILD_LEGACY) 553 set(OPT ${OPT};--cpp-std c++0x) 554 else() 555 # --cpp-std is defined by flatc default settings. 556 endif() 557 message(STATUS "`${SRC_FBS}`: add generation of C++ embedded binary schema code with '${OPT}'") 558 get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH) 559 string(REGEX REPLACE "\\.fbs$" "_bfbs_generated.h" GEN_BFBS_HEADER ${SRC_FBS}) 560 # For details about flags see generate_code.py 561 add_custom_command( 562 OUTPUT ${GEN_BFBS_HEADER} 563 COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}" 564 --cpp --gen-mutable --gen-object-api --reflect-names 565 --cpp-ptr-type flatbuffers::unique_ptr # Used to test with C++98 STLs 566 ${OPT} 567 --bfbs-comments --bfbs-builtins --bfbs-gen-embed 568 --bfbs-filenames ${SRC_FBS_DIR} 569 -I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test" 570 -o "${SRC_FBS_DIR}" 571 "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}" 572 DEPENDS flatc 573 COMMENT "Run generation: '${GEN_BFBS_HEADER}'") 574 register_generated_output(${GEN_BFBS_HEADER}) 575endfunction() 576 577if(FLATBUFFERS_BUILD_TESTS) 578 file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/tests" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}") 579 file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/samples" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}") 580 581 # TODO Add (monster_test.fbs monsterdata_test.json)->monsterdata_test.mon 582 compile_flatbuffers_schema_to_cpp(tests/monster_test.fbs) 583 compile_flatbuffers_schema_to_binary(tests/monster_test.fbs) 584 compile_flatbuffers_schema_to_cpp_opt(tests/namespace_test/namespace_test1.fbs "--no-includes;--gen-compare;--gen-name-strings") 585 compile_flatbuffers_schema_to_cpp_opt(tests/namespace_test/namespace_test2.fbs "--no-includes;--gen-compare;--gen-name-strings") 586 compile_flatbuffers_schema_to_cpp_opt(tests/union_vector/union_vector.fbs "--no-includes;--gen-compare;--gen-name-strings") 587 compile_flatbuffers_schema_to_cpp(tests/optional_scalars.fbs) 588 compile_flatbuffers_schema_to_cpp_opt(tests/native_type_test.fbs "") 589 compile_flatbuffers_schema_to_cpp_opt(tests/arrays_test.fbs "--scoped-enums;--gen-compare") 590 compile_flatbuffers_schema_to_binary(tests/arrays_test.fbs) 591 compile_flatbuffers_schema_to_embedded_binary(tests/monster_test.fbs "--no-includes;--gen-compare") 592 if(NOT (MSVC AND (MSVC_VERSION LESS 1900))) 593 compile_flatbuffers_schema_to_cpp(tests/monster_extra.fbs) # Test floating-point NAN/INF. 594 endif() 595 include_directories(${CMAKE_CURRENT_BINARY_DIR}/tests) 596 add_executable(flattests ${FlatBuffers_Tests_SRCS}) 597 target_link_libraries(flattests PRIVATE $<BUILD_INTERFACE:ProjectConfig>) 598 599 add_dependencies(flattests generated_code) 600 601 if(FLATBUFFERS_CODE_SANITIZE) 602 add_fsanitize_to_target(flattests ${FLATBUFFERS_CODE_SANITIZE}) 603 endif() 604 605 compile_flatbuffers_schema_to_cpp(samples/monster.fbs) 606 compile_flatbuffers_schema_to_binary(samples/monster.fbs) 607 include_directories(${CMAKE_CURRENT_BINARY_DIR}/samples) 608 609 add_executable(flatsamplebinary ${FlatBuffers_Sample_Binary_SRCS}) 610 target_link_libraries(flatsamplebinary PRIVATE $<BUILD_INTERFACE:ProjectConfig>) 611 add_dependencies(flatsamplebinary generated_code) 612 613 add_executable(flatsampletext ${FlatBuffers_Sample_Text_SRCS}) 614 target_link_libraries(flatsampletext PRIVATE $<BUILD_INTERFACE:ProjectConfig>) 615 add_dependencies(flatsampletext generated_code) 616 617 add_executable(flatsamplebfbs ${FlatBuffers_Sample_BFBS_SRCS}) 618 target_link_libraries(flatsamplebfbs PRIVATE $<BUILD_INTERFACE:ProjectConfig>) 619 add_dependencies(flatsamplebfbs generated_code) 620 621 if(FLATBUFFERS_BUILD_CPP17) 622 # Don't generate header for flattests_cpp17 target. 623 # This target uses "generated_cpp17/monster_test_generated.h" 624 add_executable(flattests_cpp17 ${FlatBuffers_Tests_CPP17_SRCS}) 625 add_dependencies(flattests_cpp17 generated_code) 626 target_link_libraries(flattests_cpp17 PRIVATE $<BUILD_INTERFACE:ProjectConfig>) 627 target_compile_features(flattests_cpp17 PRIVATE cxx_std_17) 628 629 if(FLATBUFFERS_CODE_SANITIZE) 630 add_fsanitize_to_target(flattests_cpp17 ${FLATBUFFERS_CODE_SANITIZE}) 631 endif() 632 endif(FLATBUFFERS_BUILD_CPP17) 633endif() 634 635if(FLATBUFFERS_BUILD_GRPCTEST) 636 if(NOT GRPC_INSTALL_PATH) 637 message(SEND_ERROR "GRPC_INSTALL_PATH variable is not defined. See grpc/README.md") 638 endif() 639 if(NOT PROTOBUF_DOWNLOAD_PATH) 640 message(SEND_ERROR "PROTOBUF_DOWNLOAD_PATH variable is not defined. See grpc/README.md") 641 endif() 642 INCLUDE_DIRECTORIES(${GRPC_INSTALL_PATH}/include) 643 INCLUDE_DIRECTORIES(${PROTOBUF_DOWNLOAD_PATH}/src) 644 find_package(Threads REQUIRED) 645 list(APPEND CMAKE_PREFIX_PATH ${GRPC_INSTALL_PATH}) 646 find_package(absl CONFIG REQUIRED) 647 find_package(protobuf CONFIG REQUIRED) 648 find_package(gRPC CONFIG REQUIRED) 649 add_executable(grpctest ${FlatBuffers_GRPCTest_SRCS}) 650 add_dependencies(grpctest generated_code) 651 target_link_libraries(grpctext 652 PRIVATE 653 $<BUILD_INTERFACE:ProjectConfig> 654 gRPC::grpc++_unsecure 655 gRPC::gpr 656 pthread 657 dl 658 ) 659endif() 660 661if(FLATBUFFERS_INSTALL) 662 include(GNUInstallDirs) 663 664 install(DIRECTORY include/flatbuffers DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) 665 666 set(FB_CMAKE_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/flatbuffers") 667 668 configure_file(CMake/FlatBuffersConfigVersion.cmake.in FlatBuffersConfigVersion.cmake @ONLY) 669 install( 670 FILES "CMake/FlatBuffersConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/FlatBuffersConfigVersion.cmake" 671 DESTINATION ${FB_CMAKE_DIR} 672 ) 673 674 if(FLATBUFFERS_BUILD_FLATLIB) 675 install( 676 TARGETS flatbuffers EXPORT FlatBuffersTargets 677 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} 678 INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} 679 ) 680 681 install(EXPORT FlatBuffersTargets 682 FILE FlatBuffersTargets.cmake 683 NAMESPACE flatbuffers:: 684 DESTINATION ${FB_CMAKE_DIR} 685 ) 686 endif() 687 688 if(FLATBUFFERS_BUILD_FLATC) 689 install( 690 TARGETS flatc EXPORT FlatcTargets 691 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} 692 ) 693 694 install( 695 EXPORT FlatcTargets 696 FILE FlatcTargets.cmake 697 NAMESPACE flatbuffers:: 698 DESTINATION ${FB_CMAKE_DIR} 699 ) 700 endif() 701 702 if(FLATBUFFERS_BUILD_SHAREDLIB) 703 install( 704 TARGETS flatbuffers_shared EXPORT FlatBuffersSharedTargets 705 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} 706 RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR} 707 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} 708 INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} 709 ) 710 711 install( 712 EXPORT FlatBuffersSharedTargets 713 FILE FlatBuffersSharedTargets.cmake 714 NAMESPACE flatbuffers:: 715 DESTINATION ${FB_CMAKE_DIR} 716 ) 717 endif() 718 719 if(FLATBUFFERS_BUILD_SHAREDLIB OR FLATBUFFERS_BUILD_FLATLIB) 720 configure_file(CMake/flatbuffers.pc.in flatbuffers.pc @ONLY) 721 install( 722 FILES "${CMAKE_CURRENT_BINARY_DIR}/flatbuffers.pc" 723 DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig 724 ) 725 endif() 726endif() 727 728if(FLATBUFFERS_BUILD_TESTS) 729 enable_testing() 730 731 add_test(NAME flattests COMMAND flattests) 732 if(FLATBUFFERS_BUILD_CPP17) 733 add_test(NAME flattests_cpp17 COMMAND flattests_cpp17) 734 endif() 735 if(FLATBUFFERS_BUILD_GRPCTEST) 736 add_test(NAME grpctest COMMAND grpctest) 737 endif() 738endif() 739 740# This target is sync-barrier. 741# Other generate-dependent targets can depend on 'generated_code' only. 742get_generated_output(fbs_generated) 743if(fbs_generated) 744 # message(STATUS "Add generated_code target with files:${fbs_generated}") 745 add_custom_target(generated_code 746 DEPENDS ${fbs_generated} 747 COMMENT "All generated files were updated.") 748endif() 749 750include(CMake/BuildFlatBuffers.cmake) 751 752if(UNIX) 753 # Use of CPack only supported on Linux systems. 754 if(FLATBUFFERS_PACKAGE_DEBIAN) 755 include(CMake/PackageDebian.cmake) 756 include(CPack) 757 endif() 758 if (FLATBUFFERS_PACKAGE_REDHAT) 759 include(CMake/PackageRedhat.cmake) 760 include(CPack) 761 endif() 762endif() 763 764# Include for running Google Benchmarks. 765if(FLATBUFFERS_BUILD_BENCHMARKS) 766 add_subdirectory(benchmarks) 767endif() 768 769# Add FlatBuffers::FlatBuffers interface, needed for FetchContent_Declare 770add_library(FlatBuffers INTERFACE) 771add_library(FlatBuffers::FlatBuffers ALIAS FlatBuffers) 772target_include_directories( 773 FlatBuffers 774 INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> 775 $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/include>) 776