1 cmake_minimum_required(VERSION 2.8.12)
2 # generate compile_commands.json
3 set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
4 include(CheckCXXSymbolExists)
8 # NOTE: Code coverage only works on Linux & OSX.
9 option(FLATBUFFERS_CODE_COVERAGE "Enable the code coverage build option." OFF)
10 option(FLATBUFFERS_BUILD_TESTS "Enable the build of tests and samples." ON)
11 option(FLATBUFFERS_INSTALL "Enable the installation of targets." ON)
12 option(FLATBUFFERS_BUILD_FLATLIB "Enable the build of the flatbuffers library"
14 option(FLATBUFFERS_BUILD_FLATC "Enable the build of the flatbuffers compiler"
16 option(FLATBUFFERS_STATIC_FLATC "Build flatbuffers compiler with -static flag"
18 option(FLATBUFFERS_BUILD_FLATHASH "Enable the build of flathash" ON)
19 option(FLATBUFFERS_BUILD_GRPCTEST "Enable the build of grpctest" OFF)
20 option(FLATBUFFERS_BUILD_SHAREDLIB
21 "Enable the build of the flatbuffers shared library"
23 option(FLATBUFFERS_LIBCXX_WITH_CLANG "Force libc++ when using Clang" ON)
24 # NOTE: Sanitizer check only works on Linux & OSX (gcc & llvm).
25 option(FLATBUFFERS_CODE_SANITIZE
26 "Add '-fsanitize' flags to 'flattests' and 'flatc' targets."
28 option(FLATBUFFERS_PACKAGE_REDHAT
29 "Build an rpm using the 'package' target."
31 option(FLATBUFFERS_PACKAGE_DEBIAN
32 "Build an deb using the 'package' target."
34 option(FLATBUFFERS_BUILD_CPP17
35 "Enable the build of c++17 test target. \"
36 Requirements: Clang6, GCC7, MSVC2017 (_MSC_VER >= 1914) or higher."
38 option(FLATBUFFERS_BUILD_LEGACY
39 "Run C++ code generator with '--cpp-std c++0x' switch."
41 option(FLATBUFFERS_ENABLE_PCH
42 "Enable precompile headers support for 'flatbuffers' and 'flatc'. \"
43 Only work if CMake supports 'target_precompile_headers'. \"
44 This can speed up compilation time."
47 if(NOT FLATBUFFERS_BUILD_FLATC AND FLATBUFFERS_BUILD_TESTS)
49 "Cannot build tests without building the compiler. Tests will be disabled.")
50 set(FLATBUFFERS_BUILD_TESTS OFF)
53 if(DEFINED FLATBUFFERS_MAX_PARSING_DEPTH)
54 # Override the default recursion depth limit.
55 add_definitions(-DFLATBUFFERS_MAX_PARSING_DEPTH=${FLATBUFFERS_MAX_PARSING_DEPTH})
56 message(STATUS "FLATBUFFERS_MAX_PARSING_DEPTH: ${FLATBUFFERS_MAX_PARSING_DEPTH}")
59 # Auto-detect locale-narrow 'strtod_l' and 'strtoull_l' functions.
60 if(NOT DEFINED FLATBUFFERS_LOCALE_INDEPENDENT)
61 set(FLATBUFFERS_LOCALE_INDEPENDENT 0)
63 check_cxx_symbol_exists(_strtof_l stdlib.h FLATBUFFERS_HAS_STRTOF_L)
64 check_cxx_symbol_exists(_strtoui64_l stdlib.h FLATBUFFERS_HAS_STRTOULL_L)
66 check_cxx_symbol_exists(strtof_l stdlib.h FLATBUFFERS_HAS_STRTOF_L)
67 check_cxx_symbol_exists(strtoull_l stdlib.h FLATBUFFERS_HAS_STRTOULL_L)
69 if(FLATBUFFERS_HAS_STRTOF_L AND FLATBUFFERS_HAS_STRTOULL_L)
70 set(FLATBUFFERS_LOCALE_INDEPENDENT 1)
73 add_definitions(-DFLATBUFFERS_LOCALE_INDEPENDENT=$<BOOL:${FLATBUFFERS_LOCALE_INDEPENDENT}>)
75 set(FlatBuffers_Library_SRCS
76 include/flatbuffers/base.h
77 include/flatbuffers/flatbuffers.h
78 include/flatbuffers/hash.h
79 include/flatbuffers/idl.h
80 include/flatbuffers/util.h
81 include/flatbuffers/reflection.h
82 include/flatbuffers/reflection_generated.h
83 include/flatbuffers/stl_emulation.h
84 include/flatbuffers/flexbuffers.h
85 include/flatbuffers/registry.h
86 include/flatbuffers/minireflect.h
93 set(FlatBuffers_Compiler_SRCS
94 ${FlatBuffers_Library_SRCS}
96 src/idl_gen_csharp.cpp
98 src/idl_gen_kotlin.cpp
103 src/idl_gen_python.cpp
104 src/idl_gen_lobster.cpp
109 src/idl_gen_json_schema.cpp
110 src/idl_gen_swift.cpp
113 include/flatbuffers/code_generators.h
114 src/code_generators.cpp
115 grpc/src/compiler/schema_interface.h
116 grpc/src/compiler/cpp_generator.h
117 grpc/src/compiler/cpp_generator.cc
118 grpc/src/compiler/go_generator.h
119 grpc/src/compiler/go_generator.cc
120 grpc/src/compiler/java_generator.h
121 grpc/src/compiler/java_generator.cc
122 grpc/src/compiler/python_generator.h
123 grpc/src/compiler/python_generator.cc
124 grpc/src/compiler/swift_generator.h
125 grpc/src/compiler/swift_generator.cc
126 grpc/src/compiler/ts_generator.h
127 grpc/src/compiler/ts_generator.cc
131 include/flatbuffers/hash.h
135 set(FlatBuffers_Tests_SRCS
136 ${FlatBuffers_Library_SRCS}
140 tests/test_assert.cpp
142 tests/test_builder.cpp
143 tests/native_type_test_impl.h
144 tests/native_type_test_impl.cpp
145 include/flatbuffers/code_generators.h
146 src/code_generators.cpp
147 # file generate by running compiler on tests/monster_test.fbs
148 ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h
149 # file generate by running compiler on namespace_test/namespace_test1.fbs
150 ${CMAKE_CURRENT_BINARY_DIR}/tests/namespace_test/namespace_test1_generated.h
151 ${CMAKE_CURRENT_BINARY_DIR}/tests/namespace_test/namespace_test2_generated.h
152 # file generate by running compiler on union_vector/union_vector.fbs
153 ${CMAKE_CURRENT_BINARY_DIR}/tests/union_vector/union_vector_generated.h
154 # file generate by running compiler on tests/arrays_test.fbs
155 ${CMAKE_CURRENT_BINARY_DIR}/tests/arrays_test_generated.h
156 # file generate by running compiler on tests/native_type_test.fbs
157 ${CMAKE_CURRENT_BINARY_DIR}/tests/native_type_test_generated.h
158 # file generate by running compiler on tests/monster_extra.fbs
159 ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_extra_generated.h
160 # file generate by running compiler on tests/monster_test.fbs
161 ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_bfbs_generated.h
162 # file generate by running compiler on tests/optional_scalars.fbs
163 ${CMAKE_CURRENT_BINARY_DIR}/tests/optional_scalars_generated.h
166 set(FlatBuffers_Tests_CPP17_SRCS
167 ${FlatBuffers_Library_SRCS}
169 tests/test_assert.cpp
170 tests/cpp17/test_cpp17.cpp
171 # file generate by running compiler on tests/monster_test.fbs
172 ${CMAKE_CURRENT_BINARY_DIR}/tests/cpp17/generated_cpp17/monster_test_generated.h
173 ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h
174 ${CMAKE_CURRENT_BINARY_DIR}/tests/cpp17/generated_cpp17/optional_scalars_generated.h
175 ${CMAKE_CURRENT_BINARY_DIR}/tests/optional_scalars_generated.h
178 set(FlatBuffers_Sample_Binary_SRCS
179 include/flatbuffers/flatbuffers.h
180 samples/sample_binary.cpp
181 # file generated by running compiler on samples/monster.fbs
182 ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h
185 set(FlatBuffers_Sample_Text_SRCS
186 ${FlatBuffers_Library_SRCS}
187 samples/sample_text.cpp
188 # file generated by running compiler on samples/monster.fbs
189 ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h
192 set(FlatBuffers_Sample_BFBS_SRCS
193 ${FlatBuffers_Library_SRCS}
194 samples/sample_bfbs.cpp
195 # file generated by running compiler on samples/monster.fbs
196 ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h
199 set(FlatBuffers_GRPCTest_SRCS
200 include/flatbuffers/flatbuffers.h
201 include/flatbuffers/grpc.h
202 include/flatbuffers/util.h
204 tests/monster_test.grpc.fb.h
207 tests/monster_test.grpc.fb.cc
208 tests/test_assert.cpp
209 tests/test_builder.cpp
210 grpc/tests/grpctest.cpp
211 grpc/tests/message_builder_test.cpp
212 # file generate by running compiler on tests/monster_test.fbs
213 ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h
216 # source_group(Compiler FILES ${FlatBuffers_Compiler_SRCS})
217 # source_group(Tests FILES ${FlatBuffers_Tests_SRCS})
219 if(EXISTS "${CMAKE_TOOLCHAIN_FILE}")
220 # do not apply any global settings if the toolchain
221 # is being configured externally
222 message(STATUS "Using toolchain file: ${CMAKE_TOOLCHAIN_FILE}.")
224 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++")
225 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Werror -Wextra -Wno-unused-parameter")
226 set(FLATBUFFERS_PRIVATE_CXX_FLAGS "-Wold-style-cast")
227 elseif(CMAKE_COMPILER_IS_GNUCXX)
230 "${CMAKE_CXX_FLAGS} -std=gnu++11")
233 "${CMAKE_CXX_FLAGS} -std=c++0x")
236 "${CMAKE_CXX_FLAGS} -Wall -pedantic -Werror -Wextra -Werror=shadow")
237 set(FLATBUFFERS_PRIVATE_CXX_FLAGS "-Wold-style-cast")
238 if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.4)
239 if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0)
241 "${CMAKE_CXX_FLAGS} -faligned-new -Werror=implicit-fallthrough=2")
244 "${CMAKE_CXX_FLAGS} -Wunused-result -Werror=unused-result -Wunused-parameter -Werror=unused-parameter")
247 # Certain platforms such as ARM do not use signed chars by default
248 # which causes issues with certain bounds checks.
250 "${CMAKE_CXX_FLAGS} -fsigned-char")
252 elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
254 "${CMAKE_CXX_FLAGS} -std=c++0x -Wall -pedantic -Werror -Wextra -Wno-unused-parameter")
255 set(FLATBUFFERS_PRIVATE_CXX_FLAGS "-Wold-style-cast")
256 if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.8)
257 list(APPEND FLATBUFFERS_PRIVATE_CXX_FLAGS "-Wimplicit-fallthrough" "-Wextra-semi" "-Werror=unused-private-field") # enable warning
259 if(FLATBUFFERS_LIBCXX_WITH_CLANG)
260 if(NOT "${CMAKE_SYSTEM_NAME}" MATCHES "Linux")
262 "${CMAKE_CXX_FLAGS} -stdlib=libc++")
264 if(NOT ("${CMAKE_SYSTEM_NAME}" MATCHES "FreeBSD" OR
265 "${CMAKE_SYSTEM_NAME}" MATCHES "Linux"))
266 set(CMAKE_EXE_LINKER_FLAGS
267 "${CMAKE_EXE_LINKER_FLAGS} -lc++abi")
271 # Certain platforms such as ARM do not use signed chars by default
272 # which causes issues with certain bounds checks.
274 "${CMAKE_CXX_FLAGS} -fsigned-char")
277 # Visual Studio pedantic build settings
278 # warning C4512: assignment operator could not be generated
279 # warning C4316: object allocated on the heap may not be aligned
280 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4 /WX /wd4512 /wd4316")
283 add_definitions("/MP")
286 # Append FLATBUFFERS_CXX_FLAGS to CMAKE_CXX_FLAGS.
287 if(DEFINED FLATBUFFERS_CXX_FLAGS AND NOT EXISTS "${CMAKE_TOOLCHAIN_FILE}")
288 message(STATUS "extend CXX_FLAGS with ${FLATBUFFERS_CXX_FLAGS}")
289 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FLATBUFFERS_CXX_FLAGS}")
291 message(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
293 if(FLATBUFFERS_CODE_COVERAGE)
294 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fprofile-arcs -ftest-coverage")
295 set(CMAKE_EXE_LINKER_FLAGS
296 "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
299 function(add_fsanitize_to_target _target _sanitizer)
301 target_compile_definitions(${_target} PRIVATE FLATBUFFERS_MEMORY_LEAK_TRACKING)
302 message(STATUS "Sanitizer MSVC::_CrtDumpMemoryLeaks added to ${_target}")
304 # FLATBUFFERS_CODE_SANITIZE: boolean {ON,OFF,YES,NO} or string with list of sanitizer.
305 # List of sanitizer is string starts with '=': "=address,undefined,thread,memory".
306 if((${CMAKE_CXX_COMPILER_ID} MATCHES "Clang") OR
307 ((${CMAKE_CXX_COMPILER_ID} MATCHES "GNU") AND NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.9"))
309 set(_sanitizer_flags "=address,undefined")
310 if(_sanitizer MATCHES "=.*")
311 # override default by user-defined sanitizer list
312 set(_sanitizer_flags ${_sanitizer})
314 target_compile_options(${_target} PRIVATE
315 -g -fsigned-char -fno-omit-frame-pointer
316 "-fsanitize${_sanitizer_flags}")
317 target_link_libraries(${_target} PRIVATE
318 "-fsanitize${_sanitizer_flags}")
319 set_property(TARGET ${_target} PROPERTY POSITION_INDEPENDENT_CODE ON)
320 message(STATUS "Sanitizer ${_sanitizer_flags} added to ${_target}")
325 function(add_pch_to_target _target _pch_header)
326 if(COMMAND target_precompile_headers)
327 target_precompile_headers(${_target} PRIVATE ${_pch_header})
329 set_source_files_properties(src/util.cpp PROPERTIES SKIP_PRECOMPILE_HEADERS ON)
335 include(biicode/cmake/biicode.cmake)
339 include_directories(include)
340 include_directories(grpc)
342 if(FLATBUFFERS_BUILD_FLATLIB)
343 add_library(flatbuffers STATIC ${FlatBuffers_Library_SRCS})
344 # Attach header directory for when build via add_subdirectory().
345 target_include_directories(flatbuffers INTERFACE
346 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)
347 target_compile_options(flatbuffers PRIVATE "${FLATBUFFERS_PRIVATE_CXX_FLAGS}")
348 if(FLATBUFFERS_ENABLE_PCH)
349 add_pch_to_target(flatbuffers include/flatbuffers/pch/pch.h)
353 if(FLATBUFFERS_BUILD_FLATC)
354 add_executable(flatc ${FlatBuffers_Compiler_SRCS})
355 if(FLATBUFFERS_ENABLE_PCH)
356 add_pch_to_target(flatc include/flatbuffers/pch/flatc_pch.h)
358 target_compile_options(flatc PRIVATE "${FLATBUFFERS_PRIVATE_CXX_FLAGS}")
359 if(FLATBUFFERS_CODE_SANITIZE AND NOT WIN32)
360 add_fsanitize_to_target(flatc ${FLATBUFFERS_CODE_SANITIZE})
362 if(NOT FLATBUFFERS_FLATC_EXECUTABLE)
363 set(FLATBUFFERS_FLATC_EXECUTABLE $<TARGET_FILE:flatc>)
366 # Make flatc.exe not depend on runtime dlls for easy distribution.
367 target_compile_options(flatc PUBLIC $<$<CONFIG:Release>:/MT>)
369 if(FLATBUFFERS_STATIC_FLATC AND NOT MSVC)
370 target_link_libraries(flatc PRIVATE -static)
374 if(FLATBUFFERS_BUILD_FLATHASH)
375 add_executable(flathash ${FlatHash_SRCS})
378 if(FLATBUFFERS_BUILD_SHAREDLIB)
379 add_library(flatbuffers_shared SHARED ${FlatBuffers_Library_SRCS})
381 # Shared object version: "major.minor.micro"
382 # - micro updated every release when there is no API/ABI changes
383 # - minor updated when there are additions in API/ABI
384 # - major (ABI number) updated when there are changes in ABI (or removals)
385 set(FlatBuffers_Library_SONAME_MAJOR "2")
386 set(FlatBuffers_Library_SONAME_FULL "${FlatBuffers_Library_SONAME_MAJOR}.0.0")
387 set_target_properties(flatbuffers_shared PROPERTIES OUTPUT_NAME flatbuffers
388 SOVERSION "${FlatBuffers_Library_SONAME_MAJOR}"
389 VERSION "${FlatBuffers_Library_SONAME_FULL}")
390 if(FLATBUFFERS_ENABLE_PCH)
391 add_pch_to_target(flatbuffers_shared include/flatbuffers/pch/pch.h)
395 # Global list of generated files.
396 # Use the global property to be independent of PARENT_SCOPE.
397 set_property(GLOBAL PROPERTY FBS_GENERATED_OUTPUTS)
399 function(get_generated_output generated_files)
400 get_property(tmp GLOBAL PROPERTY FBS_GENERATED_OUTPUTS)
401 set(${generated_files} ${tmp} PARENT_SCOPE)
402 endfunction(get_generated_output)
404 function(register_generated_output file_name)
405 get_property(tmp GLOBAL PROPERTY FBS_GENERATED_OUTPUTS)
406 list(APPEND tmp ${file_name})
407 set_property(GLOBAL PROPERTY FBS_GENERATED_OUTPUTS ${tmp})
408 endfunction(register_generated_output)
410 function(compile_flatbuffers_schema_to_cpp_opt SRC_FBS OPT)
411 if(FLATBUFFERS_BUILD_LEGACY)
412 set(OPT ${OPT};--cpp-std c++0x)
414 # --cpp-std is defined by flatc default settings.
416 message(STATUS "`${SRC_FBS}`: add generation of C++ code with '${OPT}'")
417 get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH)
418 string(REGEX REPLACE "\\.fbs$" "_generated.h" GEN_HEADER ${SRC_FBS})
421 COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}"
422 --cpp --gen-mutable --gen-object-api --reflect-names
423 --cpp-ptr-type flatbuffers::unique_ptr # Used to test with C++98 STLs
425 -I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test"
427 "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}"
429 COMMENT "Run generation: '${GEN_HEADER}'")
430 register_generated_output(${GEN_HEADER})
433 function(compile_flatbuffers_schema_to_cpp SRC_FBS)
434 compile_flatbuffers_schema_to_cpp_opt(${SRC_FBS} "--no-includes;--gen-compare")
437 function(compile_flatbuffers_schema_to_binary SRC_FBS)
438 message(STATUS "`${SRC_FBS}`: add generation of binary (.bfbs) schema")
439 get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH)
440 string(REGEX REPLACE "\\.fbs$" ".bfbs" GEN_BINARY_SCHEMA ${SRC_FBS})
441 # For details about flags see generate_code.bat(sh)
443 OUTPUT ${GEN_BINARY_SCHEMA}
444 COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}"
445 -b --schema --bfbs-comments --bfbs-builtins
446 -I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test"
448 "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}"
450 COMMENT "Run generation: '${GEN_BINARY_SCHEMA}'")
451 register_generated_output(${GEN_BINARY_SCHEMA})
454 function(compile_flatbuffers_schema_to_embedded_binary SRC_FBS OPT)
455 if(FLATBUFFERS_BUILD_LEGACY)
456 set(OPT ${OPT};--cpp-std c++0x)
458 # --cpp-std is defined by flatc default settings.
460 message(STATUS "`${SRC_FBS}`: add generation of C++ embedded binary schema code with '${OPT}'")
461 get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH)
462 string(REGEX REPLACE "\\.fbs$" "_bfbs_generated.h" GEN_BFBS_HEADER ${SRC_FBS})
463 # For details about flags see generate_code.bat(sh)
465 OUTPUT ${GEN_BFBS_HEADER}
466 COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}"
467 --cpp --gen-mutable --gen-object-api --reflect-names
468 --cpp-ptr-type flatbuffers::unique_ptr # Used to test with C++98 STLs
470 --bfbs-comments --bfbs-builtins --bfbs-gen-embed
471 -I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test"
473 "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}"
475 COMMENT "Run generation: '${GEN_BFBS_HEADER}'")
476 register_generated_output(${GEN_BFBS_HEADER})
479 if(FLATBUFFERS_BUILD_TESTS)
480 file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/tests" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
481 file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/samples" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
483 # TODO Add (monster_test.fbs monsterdata_test.json)->monsterdata_test.mon
484 compile_flatbuffers_schema_to_cpp(tests/monster_test.fbs)
485 compile_flatbuffers_schema_to_binary(tests/monster_test.fbs)
486 compile_flatbuffers_schema_to_cpp(tests/namespace_test/namespace_test1.fbs)
487 compile_flatbuffers_schema_to_cpp(tests/namespace_test/namespace_test2.fbs)
488 compile_flatbuffers_schema_to_cpp(tests/union_vector/union_vector.fbs)
489 compile_flatbuffers_schema_to_cpp(tests/optional_scalars.fbs)
490 compile_flatbuffers_schema_to_cpp_opt(tests/native_type_test.fbs "")
491 compile_flatbuffers_schema_to_cpp_opt(tests/arrays_test.fbs "--scoped-enums;--gen-compare")
492 compile_flatbuffers_schema_to_binary(tests/arrays_test.fbs)
493 compile_flatbuffers_schema_to_embedded_binary(tests/monster_test.fbs "--no-includes;--gen-compare")
494 if(NOT (MSVC AND (MSVC_VERSION LESS 1900)))
495 compile_flatbuffers_schema_to_cpp(tests/monster_extra.fbs) # Test floating-point NAN/INF.
497 include_directories(${CMAKE_CURRENT_BINARY_DIR}/tests)
498 add_executable(flattests ${FlatBuffers_Tests_SRCS})
499 add_dependencies(flattests generated_code)
500 set_property(TARGET flattests
501 PROPERTY COMPILE_DEFINITIONS FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
502 FLATBUFFERS_DEBUG_VERIFICATION_FAILURE=1)
503 if(FLATBUFFERS_CODE_SANITIZE)
504 add_fsanitize_to_target(flattests ${FLATBUFFERS_CODE_SANITIZE})
507 compile_flatbuffers_schema_to_cpp(samples/monster.fbs)
508 compile_flatbuffers_schema_to_binary(samples/monster.fbs)
509 include_directories(${CMAKE_CURRENT_BINARY_DIR}/samples)
510 add_executable(flatsamplebinary ${FlatBuffers_Sample_Binary_SRCS})
511 add_dependencies(flatsamplebinary generated_code)
512 add_executable(flatsampletext ${FlatBuffers_Sample_Text_SRCS})
513 add_dependencies(flatsampletext generated_code)
514 add_executable(flatsamplebfbs ${FlatBuffers_Sample_BFBS_SRCS})
515 add_dependencies(flatsamplebfbs generated_code)
517 if(FLATBUFFERS_BUILD_CPP17)
518 # Don't generate header for flattests_cpp17 target.
519 # This target uses "generated_cpp17/monster_test_generated.h"
520 # produced by direct call of generate_code.bat(sh) script.
521 add_executable(flattests_cpp17 ${FlatBuffers_Tests_CPP17_SRCS})
522 add_dependencies(flattests_cpp17 generated_code)
523 target_compile_features(flattests_cpp17 PRIVATE cxx_std_17)
524 target_compile_definitions(flattests_cpp17 PRIVATE
525 FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
526 FLATBUFFERS_DEBUG_VERIFICATION_FAILURE=1
528 if(FLATBUFFERS_CODE_SANITIZE)
529 add_fsanitize_to_target(flattests_cpp17 ${FLATBUFFERS_CODE_SANITIZE})
531 endif(FLATBUFFERS_BUILD_CPP17)
534 if(FLATBUFFERS_BUILD_GRPCTEST)
535 if(CMAKE_COMPILER_IS_GNUCXX)
536 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-parameter -Wno-shadow")
538 if(NOT GRPC_INSTALL_PATH)
539 message(SEND_ERROR "GRPC_INSTALL_PATH variable is not defined. See grpc/README.md")
541 if(NOT PROTOBUF_DOWNLOAD_PATH)
542 message(SEND_ERROR "PROTOBUF_DOWNLOAD_PATH variable is not defined. See grpc/README.md")
544 INCLUDE_DIRECTORIES(${GRPC_INSTALL_PATH}/include)
545 INCLUDE_DIRECTORIES(${PROTOBUF_DOWNLOAD_PATH}/src)
546 find_package(Threads REQUIRED)
547 list(APPEND CMAKE_PREFIX_PATH ${GRPC_INSTALL_PATH})
548 find_package(protobuf CONFIG REQUIRED)
549 find_package(gRPC CONFIG REQUIRED)
550 add_executable(grpctest ${FlatBuffers_GRPCTest_SRCS})
551 add_dependencies(grpctest generated_code)
552 target_link_libraries(grpctest PRIVATE gRPC::grpc++_unsecure gRPC::grpc_unsecure gRPC::gpr pthread dl)
553 if(FLATBUFFERS_CODE_SANITIZE AND NOT WIN32)
554 # GRPC test has problems with alignment and will fail under ASAN/UBSAN.
555 # add_fsanitize_to_target(grpctest ${FLATBUFFERS_CODE_SANITIZE})
559 include(CMake/Version.cmake)
561 if(FLATBUFFERS_INSTALL)
562 include(GNUInstallDirs)
564 install(DIRECTORY include/flatbuffers DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
566 set(FB_CMAKE_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/flatbuffers")
568 configure_file(CMake/FlatbuffersConfigVersion.cmake.in FlatbuffersConfigVersion.cmake @ONLY)
570 FILES "CMake/FlatbuffersConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/FlatbuffersConfigVersion.cmake"
571 DESTINATION ${FB_CMAKE_DIR}
574 if(FLATBUFFERS_BUILD_FLATLIB)
575 if(CMAKE_VERSION VERSION_LESS 3.0)
577 TARGETS flatbuffers EXPORT FlatbuffersTargets
578 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
582 TARGETS flatbuffers EXPORT FlatbuffersTargets
583 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
584 INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
588 install(EXPORT FlatbuffersTargets
589 FILE FlatbuffersTargets.cmake
590 NAMESPACE flatbuffers::
591 DESTINATION ${FB_CMAKE_DIR}
595 if(FLATBUFFERS_BUILD_FLATC)
597 TARGETS flatc EXPORT FlatcTargets
598 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
603 FILE FlatcTargets.cmake
604 NAMESPACE flatbuffers::
605 DESTINATION ${FB_CMAKE_DIR}
609 if(FLATBUFFERS_BUILD_SHAREDLIB)
610 if(CMAKE_VERSION VERSION_LESS 3.0)
612 TARGETS flatbuffers_shared EXPORT FlatbuffersSharedTargets
613 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
614 RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR}
615 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
619 TARGETS flatbuffers_shared EXPORT FlatbuffersSharedTargets
620 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
621 RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR}
622 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
623 INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
628 EXPORT FlatbuffersSharedTargets
629 FILE FlatbuffersSharedTargets.cmake
630 NAMESPACE flatbuffers::
631 DESTINATION ${FB_CMAKE_DIR}
635 if(FLATBUFFERS_BUILD_SHAREDLIB OR FLATBUFFERS_BUILD_FLATLIB)
636 configure_file(CMake/flatbuffers.pc.in flatbuffers.pc @ONLY)
638 FILES "${CMAKE_CURRENT_BINARY_DIR}/flatbuffers.pc"
639 DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
644 if(FLATBUFFERS_BUILD_TESTS)
647 add_test(NAME flattests COMMAND flattests)
648 if(FLATBUFFERS_BUILD_CPP17)
649 add_test(NAME flattests_cpp17 COMMAND flattests_cpp17)
651 if(FLATBUFFERS_BUILD_GRPCTEST)
652 add_test(NAME grpctest COMMAND grpctest)
656 # This target is sync-barrier.
657 # Other generate-dependent targets can depend on 'generated_code' only.
658 get_generated_output(fbs_generated)
660 # message(STATUS "Add generated_code target with files:${fbs_generated}")
661 add_custom_target(generated_code
662 DEPENDS ${fbs_generated}
663 COMMENT "All generated files were updated.")
666 include(CMake/BuildFlatBuffers.cmake)
669 # Use of CPack only supported on Linux systems.
670 if(FLATBUFFERS_PACKAGE_DEBIAN)
671 include(CMake/PackageDebian.cmake)
674 if (FLATBUFFERS_PACKAGE_REDHAT)
675 include(CMake/PackageRedhat.cmake)