1cmake_minimum_required(VERSION 2.8.12)
2# generate compile_commands.json
3set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
4include(CheckCXXSymbolExists)
5
6project(FlatBuffers)
7
8# NOTE: Code coverage only works on Linux & OSX.
9option(FLATBUFFERS_CODE_COVERAGE "Enable the code coverage build option." OFF)
10option(FLATBUFFERS_BUILD_TESTS "Enable the build of tests and samples." ON)
11option(FLATBUFFERS_INSTALL "Enable the installation of targets." ON)
12option(FLATBUFFERS_BUILD_FLATLIB "Enable the build of the flatbuffers library"
13       ON)
14option(FLATBUFFERS_BUILD_FLATC "Enable the build of the flatbuffers compiler"
15       ON)
16option(FLATBUFFERS_STATIC_FLATC "Build flatbuffers compiler with -static flag"
17       OFF)
18option(FLATBUFFERS_BUILD_FLATHASH "Enable the build of flathash" ON)
19option(FLATBUFFERS_BUILD_GRPCTEST "Enable the build of grpctest" OFF)
20option(FLATBUFFERS_BUILD_SHAREDLIB
21       "Enable the build of the flatbuffers shared library"
22       OFF)
23option(FLATBUFFERS_LIBCXX_WITH_CLANG "Force libc++ when using Clang" ON)
24# NOTE: Sanitizer check only works on Linux & OSX (gcc & llvm).
25option(FLATBUFFERS_CODE_SANITIZE
26      "Add '-fsanitize' flags to 'flattests' and 'flatc' targets."
27      OFF)
28option(FLATBUFFERS_PACKAGE_REDHAT
29       "Build an rpm using the 'package' target."
30       OFF)
31option(FLATBUFFERS_PACKAGE_DEBIAN
32       "Build an deb using the 'package' target."
33       OFF)
34option(FLATBUFFERS_BUILD_CPP17
35       "Enable the build of c++17 test target. \"
36       Requirements: Clang6, GCC7, MSVC2017 (_MSC_VER >= 1914)  or higher."
37       OFF)
38option(FLATBUFFERS_BUILD_LEGACY
39       "Run C++ code generator with '--cpp-std c++0x' switch."
40       OFF)
41
42if(NOT FLATBUFFERS_BUILD_FLATC AND FLATBUFFERS_BUILD_TESTS)
43    message(WARNING
44    "Cannot build tests without building the compiler. Tests will be disabled.")
45    set(FLATBUFFERS_BUILD_TESTS OFF)
46endif()
47
48if(DEFINED FLATBUFFERS_MAX_PARSING_DEPTH)
49  # Override the default recursion depth limit.
50  add_definitions(-DFLATBUFFERS_MAX_PARSING_DEPTH=${FLATBUFFERS_MAX_PARSING_DEPTH})
51  message(STATUS "FLATBUFFERS_MAX_PARSING_DEPTH: ${FLATBUFFERS_MAX_PARSING_DEPTH}")
52endif()
53
54# Auto-detect locale-narrow 'strtod_l' and  'strtoull_l' functions.
55if(NOT DEFINED FLATBUFFERS_LOCALE_INDEPENDENT)
56  set(FLATBUFFERS_LOCALE_INDEPENDENT 0)
57  if(MSVC)
58    check_cxx_symbol_exists(_strtof_l stdlib.h FLATBUFFERS_HAS_STRTOF_L)
59    check_cxx_symbol_exists(_strtoui64_l stdlib.h FLATBUFFERS_HAS_STRTOULL_L)
60  else()
61    check_cxx_symbol_exists(strtof_l stdlib.h FLATBUFFERS_HAS_STRTOF_L)
62    check_cxx_symbol_exists(strtoull_l stdlib.h FLATBUFFERS_HAS_STRTOULL_L)
63  endif()
64  if(FLATBUFFERS_HAS_STRTOF_L AND FLATBUFFERS_HAS_STRTOULL_L)
65    set(FLATBUFFERS_LOCALE_INDEPENDENT 1)
66  endif()
67endif()
68add_definitions(-DFLATBUFFERS_LOCALE_INDEPENDENT=$<BOOL:${FLATBUFFERS_LOCALE_INDEPENDENT}>)
69
70set(FlatBuffers_Library_SRCS
71  include/flatbuffers/base.h
72  include/flatbuffers/flatbuffers.h
73  include/flatbuffers/hash.h
74  include/flatbuffers/idl.h
75  include/flatbuffers/util.h
76  include/flatbuffers/reflection.h
77  include/flatbuffers/reflection_generated.h
78  include/flatbuffers/stl_emulation.h
79  include/flatbuffers/flexbuffers.h
80  include/flatbuffers/registry.h
81  include/flatbuffers/minireflect.h
82  src/idl_parser.cpp
83  src/idl_gen_text.cpp
84  src/reflection.cpp
85  src/util.cpp
86)
87
88set(FlatBuffers_Compiler_SRCS
89  ${FlatBuffers_Library_SRCS}
90  src/idl_gen_cpp.cpp
91  src/idl_gen_csharp.cpp
92  src/idl_gen_dart.cpp
93  src/idl_gen_kotlin.cpp
94  src/idl_gen_go.cpp
95  src/idl_gen_java.cpp
96  src/idl_gen_js_ts.cpp
97  src/idl_gen_php.cpp
98  src/idl_gen_python.cpp
99  src/idl_gen_lobster.cpp
100  src/idl_gen_lua.cpp
101  src/idl_gen_rust.cpp
102  src/idl_gen_fbs.cpp
103  src/idl_gen_grpc.cpp
104  src/idl_gen_json_schema.cpp
105  src/idl_gen_swift.cpp
106  src/flatc.cpp
107  src/flatc_main.cpp
108  include/flatbuffers/code_generators.h
109  src/code_generators.cpp
110  grpc/src/compiler/schema_interface.h
111  grpc/src/compiler/cpp_generator.h
112  grpc/src/compiler/cpp_generator.cc
113  grpc/src/compiler/go_generator.h
114  grpc/src/compiler/go_generator.cc
115  grpc/src/compiler/java_generator.h
116  grpc/src/compiler/java_generator.cc
117  grpc/src/compiler/python_generator.h
118  grpc/src/compiler/python_private_generator.h
119  grpc/src/compiler/python_generator.cc
120  grpc/src/compiler/swift_generator.h
121  grpc/src/compiler/swift_generator.cc
122)
123
124set(FlatHash_SRCS
125  include/flatbuffers/hash.h
126  src/flathash.cpp
127)
128
129set(FlatBuffers_Tests_SRCS
130  ${FlatBuffers_Library_SRCS}
131  src/idl_gen_fbs.cpp
132  tests/test.cpp
133  tests/test_assert.h
134  tests/test_assert.cpp
135  tests/test_builder.h
136  tests/test_builder.cpp
137  tests/native_type_test_impl.h
138  tests/native_type_test_impl.cpp
139  include/flatbuffers/code_generators.h
140  src/code_generators.cpp
141  # file generate by running compiler on tests/monster_test.fbs
142  ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h
143  # file generate by running compiler on namespace_test/namespace_test1.fbs
144  ${CMAKE_CURRENT_BINARY_DIR}/tests/namespace_test/namespace_test1_generated.h
145  ${CMAKE_CURRENT_BINARY_DIR}/tests/namespace_test/namespace_test2_generated.h
146  # file generate by running compiler on union_vector/union_vector.fbs
147  ${CMAKE_CURRENT_BINARY_DIR}/tests/union_vector/union_vector_generated.h
148  # file generate by running compiler on tests/arrays_test.fbs
149  ${CMAKE_CURRENT_BINARY_DIR}/tests/arrays_test_generated.h
150  # file generate by running compiler on tests/native_type_test.fbs
151  ${CMAKE_CURRENT_BINARY_DIR}/tests/native_type_test_generated.h
152  # file generate by running compiler on tests/monster_extra.fbs
153  ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_extra_generated.h
154  # file generate by running compiler on tests/monster_test.fbs
155  ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_bfbs_generated.h
156)
157
158set(FlatBuffers_Tests_CPP17_SRCS
159  ${FlatBuffers_Library_SRCS}
160  tests/test_assert.h
161  tests/test_assert.cpp
162  tests/cpp17/test_cpp17.cpp
163  # file generate by running compiler on tests/monster_test.fbs
164  ${CMAKE_CURRENT_BINARY_DIR}/tests/cpp17/generated_cpp17/monster_test_generated.h
165  ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h
166)
167
168set(FlatBuffers_Sample_Binary_SRCS
169  include/flatbuffers/flatbuffers.h
170  samples/sample_binary.cpp
171  # file generated by running compiler on samples/monster.fbs
172  ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h
173)
174
175set(FlatBuffers_Sample_Text_SRCS
176  ${FlatBuffers_Library_SRCS}
177  samples/sample_text.cpp
178  # file generated by running compiler on samples/monster.fbs
179  ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h
180)
181
182set(FlatBuffers_Sample_BFBS_SRCS
183  ${FlatBuffers_Library_SRCS}
184  samples/sample_bfbs.cpp
185  # file generated by running compiler on samples/monster.fbs
186  ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h
187)
188
189set(FlatBuffers_GRPCTest_SRCS
190  include/flatbuffers/flatbuffers.h
191  include/flatbuffers/grpc.h
192  include/flatbuffers/util.h
193  src/util.cpp
194  tests/monster_test.grpc.fb.h
195  tests/test_assert.h
196  tests/test_builder.h
197  tests/monster_test.grpc.fb.cc
198  tests/test_assert.cpp
199  tests/test_builder.cpp
200  grpc/tests/grpctest.cpp
201  grpc/tests/message_builder_test.cpp
202  # file generate by running compiler on tests/monster_test.fbs
203  ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h
204)
205
206# source_group(Compiler FILES ${FlatBuffers_Compiler_SRCS})
207# source_group(Tests FILES ${FlatBuffers_Tests_SRCS})
208
209if(EXISTS "${CMAKE_TOOLCHAIN_FILE}")
210  # do not apply any global settings if the toolchain
211  # is being configured externally
212  message(STATUS "Using toolchain file: ${CMAKE_TOOLCHAIN_FILE}.")
213elseif(APPLE)
214  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++")
215  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Werror -Wextra -Wno-unused-parameter")
216  set(FLATBUFFERS_PRIVATE_CXX_FLAGS "-Wold-style-cast")
217elseif(CMAKE_COMPILER_IS_GNUCXX)
218  if(CYGWIN)
219    set(CMAKE_CXX_FLAGS
220      "${CMAKE_CXX_FLAGS} -std=gnu++11")
221  else(CYGWIN)
222    set(CMAKE_CXX_FLAGS
223      "${CMAKE_CXX_FLAGS} -std=c++0x")
224  endif(CYGWIN)
225  set(CMAKE_CXX_FLAGS
226    "${CMAKE_CXX_FLAGS} -Wall -pedantic -Werror -Wextra -Werror=shadow")
227  set(FLATBUFFERS_PRIVATE_CXX_FLAGS "-Wold-style-cast")
228  if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.4)
229    if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0)
230      set(CMAKE_CXX_FLAGS
231        "${CMAKE_CXX_FLAGS} -faligned-new -Werror=implicit-fallthrough=2")
232    endif()
233    set(CMAKE_CXX_FLAGS
234      "${CMAKE_CXX_FLAGS} -Wunused-result -Werror=unused-result -Wunused-parameter -Werror=unused-parameter")
235  endif()
236
237  # Certain platforms such as ARM do not use signed chars by default
238  # which causes issues with certain bounds checks.
239  set(CMAKE_CXX_FLAGS
240    "${CMAKE_CXX_FLAGS} -fsigned-char")
241
242elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
243  set(CMAKE_CXX_FLAGS
244      "${CMAKE_CXX_FLAGS} -std=c++0x -Wall -pedantic -Werror -Wextra -Wno-unused-parameter")
245  set(FLATBUFFERS_PRIVATE_CXX_FLAGS "-Wold-style-cast")
246  if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.8)
247    list(APPEND FLATBUFFERS_PRIVATE_CXX_FLAGS "-Wimplicit-fallthrough" "-Wextra-semi" "-Werror=unused-private-field") # enable warning
248  endif()
249  if(FLATBUFFERS_LIBCXX_WITH_CLANG)
250    if(NOT "${CMAKE_SYSTEM_NAME}" MATCHES "Linux")
251      set(CMAKE_CXX_FLAGS
252          "${CMAKE_CXX_FLAGS} -stdlib=libc++")
253    endif()
254    if(NOT ("${CMAKE_SYSTEM_NAME}" MATCHES "FreeBSD" OR
255            "${CMAKE_SYSTEM_NAME}" MATCHES "Linux"))
256      set(CMAKE_EXE_LINKER_FLAGS
257          "${CMAKE_EXE_LINKER_FLAGS} -lc++abi")
258    endif()
259  endif()
260
261  # Certain platforms such as ARM do not use signed chars by default
262  # which causes issues with certain bounds checks.
263  set(CMAKE_CXX_FLAGS
264    "${CMAKE_CXX_FLAGS} -fsigned-char")
265
266elseif(MSVC)
267  # Visual Studio pedantic build settings
268  # warning C4512: assignment operator could not be generated
269  # warning C4316: object allocated on the heap may not be aligned
270  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4 /WX /wd4512 /wd4316")
271endif()
272
273if(FLATBUFFERS_CODE_COVERAGE)
274  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fprofile-arcs -ftest-coverage")
275  set(CMAKE_EXE_LINKER_FLAGS
276      "${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
277endif()
278
279function(add_fsanitize_to_target _target _sanitizer)
280  if(WIN32)
281    target_compile_definitions(${_target} PRIVATE FLATBUFFERS_MEMORY_LEAK_TRACKING)
282    message(STATUS "Sanitizer MSVC::_CrtDumpMemoryLeaks added to ${_target}")
283  else()
284    # FLATBUFFERS_CODE_SANITIZE: boolean {ON,OFF,YES,NO} or string with list of sanitizer.
285    # List of sanitizer is string starts with '=': "=address,undefined,thread,memory".
286    if((${CMAKE_CXX_COMPILER_ID} MATCHES "Clang") OR
287      ((${CMAKE_CXX_COMPILER_ID} MATCHES "GNU") AND NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.9"))
288    )
289      set(_sanitizer_flags "=address,undefined")
290      if(_sanitizer MATCHES "=.*")
291        # override default by user-defined sanitizer list
292        set(_sanitizer_flags ${_sanitizer})
293      endif()
294      target_compile_options(${_target} PRIVATE
295        -g -fsigned-char -fno-omit-frame-pointer
296        "-fsanitize${_sanitizer_flags}")
297      target_link_libraries(${_target} PRIVATE
298        "-fsanitize${_sanitizer_flags}")
299      set_property(TARGET ${_target} PROPERTY POSITION_INDEPENDENT_CODE ON)
300      message(STATUS "Sanitizer ${_sanitizer_flags} added to ${_target}")
301    endif()
302  endif()
303endfunction()
304
305if(BIICODE)
306  include(biicode/cmake/biicode.cmake)
307  return()
308endif()
309
310include_directories(include)
311include_directories(grpc)
312
313if(FLATBUFFERS_BUILD_FLATLIB)
314  add_library(flatbuffers STATIC ${FlatBuffers_Library_SRCS})
315  # Attach header directory for when build via add_subdirectory().
316  target_include_directories(flatbuffers INTERFACE
317    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)
318  target_compile_options(flatbuffers PRIVATE "${FLATBUFFERS_PRIVATE_CXX_FLAGS}")
319endif()
320
321if(FLATBUFFERS_BUILD_FLATC)
322  add_executable(flatc ${FlatBuffers_Compiler_SRCS})
323  target_compile_options(flatc PRIVATE "${FLATBUFFERS_PRIVATE_CXX_FLAGS}")
324  if(FLATBUFFERS_CODE_SANITIZE AND NOT WIN32)
325    add_fsanitize_to_target(flatc ${FLATBUFFERS_CODE_SANITIZE})
326  endif()
327  if(NOT FLATBUFFERS_FLATC_EXECUTABLE)
328    set(FLATBUFFERS_FLATC_EXECUTABLE $<TARGET_FILE:flatc>)
329  endif()
330  if(MSVC)
331    # Make flatc.exe not depend on runtime dlls for easy distribution.
332    target_compile_options(flatc PUBLIC $<$<CONFIG:Release>:/MT>)
333  endif()
334  if(FLATBUFFERS_STATIC_FLATC AND NOT MSVC)
335    target_link_libraries(flatc PRIVATE -static)
336  endif()
337endif()
338
339if(FLATBUFFERS_BUILD_FLATHASH)
340  add_executable(flathash ${FlatHash_SRCS})
341endif()
342
343if(FLATBUFFERS_BUILD_SHAREDLIB)
344  add_library(flatbuffers_shared SHARED ${FlatBuffers_Library_SRCS})
345
346  # Shared object version: "major.minor.micro"
347  # - micro updated every release when there is no API/ABI changes
348  # - minor updated when there are additions in API/ABI
349  # - major (ABI number) updated when there are changes in ABI (or removals)
350  set(FlatBuffers_Library_SONAME_MAJOR "1")
351  set(FlatBuffers_Library_SONAME_FULL "${FlatBuffers_Library_SONAME_MAJOR}.12.0")
352  set_target_properties(flatbuffers_shared PROPERTIES OUTPUT_NAME flatbuffers
353                        SOVERSION "${FlatBuffers_Library_SONAME_MAJOR}"
354                        VERSION "${FlatBuffers_Library_SONAME_FULL}")
355endif()
356
357# Global list of generated files.
358# Use the global property to be independent of PARENT_SCOPE.
359set_property(GLOBAL PROPERTY FBS_GENERATED_OUTPUTS)
360
361function(get_generated_output generated_files)
362  get_property(tmp GLOBAL PROPERTY FBS_GENERATED_OUTPUTS)
363  set(${generated_files} ${tmp} PARENT_SCOPE)
364endfunction(get_generated_output)
365
366function(register_generated_output file_name)
367  get_property(tmp GLOBAL PROPERTY FBS_GENERATED_OUTPUTS)
368  list(APPEND tmp ${file_name})
369  set_property(GLOBAL PROPERTY FBS_GENERATED_OUTPUTS ${tmp})
370endfunction(register_generated_output)
371
372function(compile_flatbuffers_schema_to_cpp_opt SRC_FBS OPT)
373  if(FLATBUFFERS_BUILD_LEGACY)
374    set(OPT ${OPT};--cpp-std c++0x)
375  else()
376    # --cpp-std is defined by flatc default settings.
377  endif()
378  message(STATUS "`${SRC_FBS}`: add generation of C++ code with '${OPT}'")
379  get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH)
380  string(REGEX REPLACE "\\.fbs$" "_generated.h" GEN_HEADER ${SRC_FBS})
381  add_custom_command(
382    OUTPUT ${GEN_HEADER}
383    COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}"
384            --cpp --gen-mutable --gen-object-api --reflect-names
385            --cpp-ptr-type flatbuffers::unique_ptr # Used to test with C++98 STLs
386            ${OPT}
387            -I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test"
388            -o "${SRC_FBS_DIR}"
389            "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}"
390    DEPENDS flatc
391    COMMENT "Run generation: '${GEN_HEADER}'")
392  register_generated_output(${GEN_HEADER})
393endfunction()
394
395function(compile_flatbuffers_schema_to_cpp SRC_FBS)
396  compile_flatbuffers_schema_to_cpp_opt(${SRC_FBS} "--no-includes;--gen-compare")
397endfunction()
398
399function(compile_flatbuffers_schema_to_binary SRC_FBS)
400  message(STATUS "`${SRC_FBS}`: add generation of binary (.bfbs) schema")
401  get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH)
402  string(REGEX REPLACE "\\.fbs$" ".bfbs" GEN_BINARY_SCHEMA ${SRC_FBS})
403  # For details about flags see generate_code.bat(sh)
404  add_custom_command(
405    OUTPUT ${GEN_BINARY_SCHEMA}
406    COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}"
407            -b --schema --bfbs-comments --bfbs-builtins
408            -I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test"
409            -o "${SRC_FBS_DIR}"
410            "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}"
411    DEPENDS flatc
412    COMMENT "Run generation: '${GEN_BINARY_SCHEMA}'")
413  register_generated_output(${GEN_BINARY_SCHEMA})
414endfunction()
415
416function(compile_flatbuffers_schema_to_embedded_binary SRC_FBS OPT)
417  if(FLATBUFFERS_BUILD_LEGACY)
418    set(OPT ${OPT};--cpp-std c++0x)
419  else()
420    # --cpp-std is defined by flatc default settings.
421  endif()
422  message(STATUS "`${SRC_FBS}`: add generation of C++ embedded binary schema code with '${OPT}'")
423  get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH)
424  string(REGEX REPLACE "\\.fbs$" "_bfbs_generated.h" GEN_BFBS_HEADER ${SRC_FBS})
425  # For details about flags see generate_code.bat(sh)
426  add_custom_command(
427          OUTPUT ${GEN_BFBS_HEADER}
428          COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}"
429          --cpp --gen-mutable --gen-object-api --reflect-names
430          --cpp-ptr-type flatbuffers::unique_ptr # Used to test with C++98 STLs
431          ${OPT}
432          --bfbs-comments --bfbs-builtins --bfbs-gen-embed
433          -I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test"
434          -o "${SRC_FBS_DIR}"
435          "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}"
436          DEPENDS flatc
437          COMMENT "Run generation: '${GEN_BFBS_HEADER}'")
438  register_generated_output(${GEN_BFBS_HEADER})
439endfunction()
440
441if(FLATBUFFERS_BUILD_TESTS)
442  file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/tests" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
443  file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/samples" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
444
445  # TODO Add (monster_test.fbs monsterdata_test.json)->monsterdata_test.mon
446  compile_flatbuffers_schema_to_cpp(tests/monster_test.fbs)
447  compile_flatbuffers_schema_to_binary(tests/monster_test.fbs)
448  compile_flatbuffers_schema_to_cpp(tests/namespace_test/namespace_test1.fbs)
449  compile_flatbuffers_schema_to_cpp(tests/namespace_test/namespace_test2.fbs)
450  compile_flatbuffers_schema_to_cpp(tests/union_vector/union_vector.fbs)
451  compile_flatbuffers_schema_to_cpp_opt(tests/native_type_test.fbs "")
452  compile_flatbuffers_schema_to_cpp_opt(tests/arrays_test.fbs "--scoped-enums;--gen-compare")
453  compile_flatbuffers_schema_to_binary(tests/arrays_test.fbs)
454  compile_flatbuffers_schema_to_embedded_binary(tests/monster_test.fbs "--no-includes;--gen-compare")
455  if(NOT (MSVC AND (MSVC_VERSION LESS 1900)))
456    compile_flatbuffers_schema_to_cpp(tests/monster_extra.fbs) # Test floating-point NAN/INF.
457  endif()
458  include_directories(${CMAKE_CURRENT_BINARY_DIR}/tests)
459  add_executable(flattests ${FlatBuffers_Tests_SRCS})
460  add_dependencies(flattests generated_code)
461  set_property(TARGET flattests
462    PROPERTY COMPILE_DEFINITIONS FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
463    FLATBUFFERS_DEBUG_VERIFICATION_FAILURE=1)
464  if(FLATBUFFERS_CODE_SANITIZE)
465    add_fsanitize_to_target(flattests ${FLATBUFFERS_CODE_SANITIZE})
466  endif()
467
468  compile_flatbuffers_schema_to_cpp(samples/monster.fbs)
469  compile_flatbuffers_schema_to_binary(samples/monster.fbs)
470  include_directories(${CMAKE_CURRENT_BINARY_DIR}/samples)
471  add_executable(flatsamplebinary ${FlatBuffers_Sample_Binary_SRCS})
472  add_dependencies(flatsamplebinary generated_code)
473  add_executable(flatsampletext ${FlatBuffers_Sample_Text_SRCS})
474  add_dependencies(flatsampletext generated_code)
475  add_executable(flatsamplebfbs ${FlatBuffers_Sample_BFBS_SRCS})
476  add_dependencies(flatsamplebfbs generated_code)
477
478  if(FLATBUFFERS_BUILD_CPP17)
479    # Don't generate header for flattests_cpp17 target.
480    # This target uses "generated_cpp17/monster_test_generated.h"
481    # produced by direct call of generate_code.bat(sh) script.
482    add_executable(flattests_cpp17 ${FlatBuffers_Tests_CPP17_SRCS})
483    add_dependencies(flattests_cpp17 generated_code)
484    target_compile_features(flattests_cpp17 PRIVATE cxx_std_17)
485    target_compile_definitions(flattests_cpp17 PRIVATE
486      FLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
487      FLATBUFFERS_DEBUG_VERIFICATION_FAILURE=1
488    )
489    if(FLATBUFFERS_CODE_SANITIZE)
490      add_fsanitize_to_target(flattests_cpp17 ${FLATBUFFERS_CODE_SANITIZE})
491    endif()
492  endif(FLATBUFFERS_BUILD_CPP17)
493endif()
494
495if(FLATBUFFERS_BUILD_GRPCTEST)
496  if(CMAKE_COMPILER_IS_GNUCXX)
497    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-parameter -Wno-shadow")
498  endif()
499  if(NOT GRPC_INSTALL_PATH)
500    message(SEND_ERROR "GRPC_INSTALL_PATH variable is not defined. See grpc/README.md")
501  endif()
502  if(NOT PROTOBUF_DOWNLOAD_PATH)
503    message(SEND_ERROR "PROTOBUF_DOWNLOAD_PATH variable is not defined. See grpc/README.md")
504  endif()
505  INCLUDE_DIRECTORIES(${GRPC_INSTALL_PATH}/include)
506  INCLUDE_DIRECTORIES(${PROTOBUF_DOWNLOAD_PATH}/src)
507  LINK_DIRECTORIES(${GRPC_INSTALL_PATH}/lib)
508  add_executable(grpctest ${FlatBuffers_GRPCTest_SRCS})
509  add_dependencies(grpctest generated_code)
510  target_link_libraries(grpctest PRIVATE grpc++_unsecure grpc_unsecure gpr pthread dl)
511  if(FLATBUFFERS_CODE_SANITIZE AND NOT WIN32)
512    # GRPC test has problems with alignment and will fail under ASAN/UBSAN.
513    # add_fsanitize_to_target(grpctest ${FLATBUFFERS_CODE_SANITIZE})
514  endif()
515endif()
516
517include(CMake/Version.cmake)
518
519if(FLATBUFFERS_INSTALL)
520  include(GNUInstallDirs)
521
522  install(DIRECTORY include/flatbuffers DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
523
524  set(FB_CMAKE_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/flatbuffers")
525
526  configure_file(CMake/FlatbuffersConfigVersion.cmake.in FlatbuffersConfigVersion.cmake @ONLY)
527  install(
528      FILES "CMake/FlatbuffersConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/FlatbuffersConfigVersion.cmake"
529      DESTINATION ${FB_CMAKE_DIR}
530  )
531
532  if(FLATBUFFERS_BUILD_FLATLIB)
533    if(CMAKE_VERSION VERSION_LESS 3.0)
534      install(
535        TARGETS flatbuffers EXPORT FlatbuffersTargets
536        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
537      )
538    else()
539      install(
540        TARGETS flatbuffers EXPORT FlatbuffersTargets
541        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
542        INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
543      )
544    endif()
545
546    install(EXPORT FlatbuffersTargets
547      FILE FlatbuffersTargets.cmake
548      NAMESPACE flatbuffers::
549      DESTINATION ${FB_CMAKE_DIR}
550    )
551  endif()
552
553  if(FLATBUFFERS_BUILD_FLATC)
554    install(
555      TARGETS flatc EXPORT FlatcTargets
556      RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
557    )
558
559    install(
560      EXPORT FlatcTargets
561      FILE FlatcTargets.cmake
562      NAMESPACE flatbuffers::
563      DESTINATION ${FB_CMAKE_DIR}
564    )
565  endif()
566
567  if(FLATBUFFERS_BUILD_SHAREDLIB)
568    if(CMAKE_VERSION VERSION_LESS 3.0)
569      install(
570        TARGETS flatbuffers_shared EXPORT FlatbuffersSharedTargets
571        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
572        RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR}
573        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
574      )
575    else()
576      install(
577        TARGETS flatbuffers_shared EXPORT FlatbuffersSharedTargets
578        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
579        RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR}
580        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
581        INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
582      )
583    endif()
584
585  install(
586      EXPORT FlatbuffersSharedTargets
587      FILE FlatbuffersSharedTargets.cmake
588      NAMESPACE flatbuffers::
589      DESTINATION ${FB_CMAKE_DIR}
590    )
591  endif()
592endif()
593
594if(FLATBUFFERS_BUILD_TESTS)
595  enable_testing()
596
597  add_test(NAME flattests COMMAND flattests)
598  if(FLATBUFFERS_BUILD_CPP17)
599    add_test(NAME flattests_cpp17 COMMAND flattests_cpp17)
600  endif()
601  if(FLATBUFFERS_BUILD_GRPCTEST)
602    add_test(NAME grpctest COMMAND grpctest)
603  endif()
604endif()
605
606# This target is sync-barrier.
607# Other generate-dependent targets can depend on 'generated_code' only.
608get_generated_output(fbs_generated)
609if(fbs_generated)
610  # message(STATUS "Add generated_code target with files:${fbs_generated}")
611  add_custom_target(generated_code
612    DEPENDS ${fbs_generated}
613    COMMENT "All generated files were updated.")
614endif()
615
616include(CMake/BuildFlatBuffers.cmake)
617
618if(UNIX)
619    # Use of CPack only supported on Linux systems.
620    if(FLATBUFFERS_PACKAGE_DEBIAN)
621        include(CMake/PackageDebian.cmake)
622    endif()
623    if (FLATBUFFERS_PACKAGE_REDHAT)
624        include(CMake/PackageRedhat.cmake)
625    endif()
626    include(CPack)
627endif()
628