More missing version changes
[platform/upstream/flatbuffers.git] / CMakeLists.txt
1 cmake_minimum_required(VERSION 2.8.12)
2 # generate compile_commands.json
3 set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
4 include(CheckCXXSymbolExists)
5
6 project(FlatBuffers)
7
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"
13        ON)
14 option(FLATBUFFERS_BUILD_FLATC "Enable the build of the flatbuffers compiler"
15        ON)
16 option(FLATBUFFERS_STATIC_FLATC "Build flatbuffers compiler with -static flag"
17        OFF)
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"
22        OFF)
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."
27       OFF)
28 option(FLATBUFFERS_PACKAGE_REDHAT
29        "Build an rpm using the 'package' target."
30        OFF)
31 option(FLATBUFFERS_PACKAGE_DEBIAN
32        "Build an deb using the 'package' target."
33        OFF)
34 option(FLATBUFFERS_BUILD_CPP17
35        "Enable the build of c++17 test target. \"
36        Requirements: Clang6, GCC7, MSVC2017 (_MSC_VER >= 1914)  or higher."
37        OFF)
38 option(FLATBUFFERS_BUILD_LEGACY
39        "Run C++ code generator with '--cpp-std c++0x' switch."
40        OFF)
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."
45        OFF)
46
47 if(NOT FLATBUFFERS_BUILD_FLATC AND FLATBUFFERS_BUILD_TESTS)
48     message(WARNING
49     "Cannot build tests without building the compiler. Tests will be disabled.")
50     set(FLATBUFFERS_BUILD_TESTS OFF)
51 endif()
52
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}")
57 endif()
58
59 # Auto-detect locale-narrow 'strtod_l' and  'strtoull_l' functions.
60 if(NOT DEFINED FLATBUFFERS_LOCALE_INDEPENDENT)
61   set(FLATBUFFERS_LOCALE_INDEPENDENT 0)
62   if(MSVC)
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)
65   else()
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)
68   endif()
69   if(FLATBUFFERS_HAS_STRTOF_L AND FLATBUFFERS_HAS_STRTOULL_L)
70     set(FLATBUFFERS_LOCALE_INDEPENDENT 1)
71   endif()
72 endif()
73 add_definitions(-DFLATBUFFERS_LOCALE_INDEPENDENT=$<BOOL:${FLATBUFFERS_LOCALE_INDEPENDENT}>)
74
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
87   src/idl_parser.cpp
88   src/idl_gen_text.cpp
89   src/reflection.cpp
90   src/util.cpp
91 )
92
93 set(FlatBuffers_Compiler_SRCS
94   ${FlatBuffers_Library_SRCS}
95   src/idl_gen_cpp.cpp
96   src/idl_gen_csharp.cpp
97   src/idl_gen_dart.cpp
98   src/idl_gen_kotlin.cpp
99   src/idl_gen_go.cpp
100   src/idl_gen_java.cpp
101   src/idl_gen_ts.cpp
102   src/idl_gen_php.cpp
103   src/idl_gen_python.cpp
104   src/idl_gen_lobster.cpp
105   src/idl_gen_lua.cpp
106   src/idl_gen_rust.cpp
107   src/idl_gen_fbs.cpp
108   src/idl_gen_grpc.cpp
109   src/idl_gen_json_schema.cpp
110   src/idl_gen_swift.cpp
111   src/flatc.cpp
112   src/flatc_main.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
128 )
129
130 set(FlatHash_SRCS
131   include/flatbuffers/hash.h
132   src/flathash.cpp
133 )
134
135 set(FlatBuffers_Tests_SRCS
136   ${FlatBuffers_Library_SRCS}
137   src/idl_gen_fbs.cpp
138   tests/test.cpp
139   tests/test_assert.h
140   tests/test_assert.cpp
141   tests/test_builder.h
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
164 )
165
166 set(FlatBuffers_Tests_CPP17_SRCS
167   ${FlatBuffers_Library_SRCS}
168   tests/test_assert.h
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
176 )
177
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
183 )
184
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
190 )
191
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
197 )
198
199 set(FlatBuffers_GRPCTest_SRCS
200   include/flatbuffers/flatbuffers.h
201   include/flatbuffers/grpc.h
202   include/flatbuffers/util.h
203   src/util.cpp
204   tests/monster_test.grpc.fb.h
205   tests/test_assert.h
206   tests/test_builder.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
214 )
215
216 # source_group(Compiler FILES ${FlatBuffers_Compiler_SRCS})
217 # source_group(Tests FILES ${FlatBuffers_Tests_SRCS})
218
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}.")
223 elseif(APPLE)
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)
228   if(CYGWIN)
229     set(CMAKE_CXX_FLAGS
230       "${CMAKE_CXX_FLAGS} -std=gnu++11")
231   else(CYGWIN)
232     set(CMAKE_CXX_FLAGS
233       "${CMAKE_CXX_FLAGS} -std=c++0x")
234   endif(CYGWIN)
235   set(CMAKE_CXX_FLAGS
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)
240       set(CMAKE_CXX_FLAGS
241         "${CMAKE_CXX_FLAGS} -faligned-new -Werror=implicit-fallthrough=2")
242     endif()
243     set(CMAKE_CXX_FLAGS
244       "${CMAKE_CXX_FLAGS} -Wunused-result -Werror=unused-result -Wunused-parameter -Werror=unused-parameter")
245   endif()
246
247   # Certain platforms such as ARM do not use signed chars by default
248   # which causes issues with certain bounds checks.
249   set(CMAKE_CXX_FLAGS
250     "${CMAKE_CXX_FLAGS} -fsigned-char")
251
252 elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
253   set(CMAKE_CXX_FLAGS
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
258   endif()
259   if(FLATBUFFERS_LIBCXX_WITH_CLANG)
260     if(NOT "${CMAKE_SYSTEM_NAME}" MATCHES "Linux")
261       set(CMAKE_CXX_FLAGS
262           "${CMAKE_CXX_FLAGS} -stdlib=libc++")
263     endif()
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")
268     endif()
269   endif()
270
271   # Certain platforms such as ARM do not use signed chars by default
272   # which causes issues with certain bounds checks.
273   set(CMAKE_CXX_FLAGS
274     "${CMAKE_CXX_FLAGS} -fsigned-char")
275
276 elseif(MSVC)
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")
281
282   # multi-core build.
283   add_definitions("/MP")
284 endif()
285
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}")
290 endif()
291 message(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
292
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")
297 endif()
298
299 function(add_fsanitize_to_target _target _sanitizer)
300   if(WIN32)
301     target_compile_definitions(${_target} PRIVATE FLATBUFFERS_MEMORY_LEAK_TRACKING)
302     message(STATUS "Sanitizer MSVC::_CrtDumpMemoryLeaks added to ${_target}")
303   else()
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"))
308     )
309       set(_sanitizer_flags "=address,undefined")
310       if(_sanitizer MATCHES "=.*")
311         # override default by user-defined sanitizer list
312         set(_sanitizer_flags ${_sanitizer})
313       endif()
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}")
321     endif()
322   endif()
323 endfunction()
324
325 function(add_pch_to_target _target _pch_header)
326   if(COMMAND target_precompile_headers)
327     target_precompile_headers(${_target} PRIVATE ${_pch_header})
328     if(NOT MSVC)
329       set_source_files_properties(src/util.cpp PROPERTIES SKIP_PRECOMPILE_HEADERS ON)
330     endif()
331   endif()
332 endfunction()
333
334 if(BIICODE)
335   include(biicode/cmake/biicode.cmake)
336   return()
337 endif()
338
339 include_directories(include)
340 include_directories(grpc)
341
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)
350   endif()
351 endif()
352
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)
357   endif()
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})
361   endif()
362   if(NOT FLATBUFFERS_FLATC_EXECUTABLE)
363     set(FLATBUFFERS_FLATC_EXECUTABLE $<TARGET_FILE:flatc>)
364   endif()
365   if(MSVC)
366     # Make flatc.exe not depend on runtime dlls for easy distribution.
367     target_compile_options(flatc PUBLIC $<$<CONFIG:Release>:/MT>)
368   endif()
369   if(FLATBUFFERS_STATIC_FLATC AND NOT MSVC)
370     target_link_libraries(flatc PRIVATE -static)
371   endif()
372 endif()
373
374 if(FLATBUFFERS_BUILD_FLATHASH)
375   add_executable(flathash ${FlatHash_SRCS})
376 endif()
377
378 if(FLATBUFFERS_BUILD_SHAREDLIB)
379   add_library(flatbuffers_shared SHARED ${FlatBuffers_Library_SRCS})
380
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)
392   endif()
393 endif()
394
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)
398
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)
403
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)
409
410 function(compile_flatbuffers_schema_to_cpp_opt SRC_FBS OPT)
411   if(FLATBUFFERS_BUILD_LEGACY)
412     set(OPT ${OPT};--cpp-std c++0x)
413   else()
414     # --cpp-std is defined by flatc default settings.
415   endif()
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})
419   add_custom_command(
420     OUTPUT ${GEN_HEADER}
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
424             ${OPT}
425             -I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test"
426             -o "${SRC_FBS_DIR}"
427             "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}"
428     DEPENDS flatc
429     COMMENT "Run generation: '${GEN_HEADER}'")
430   register_generated_output(${GEN_HEADER})
431 endfunction()
432
433 function(compile_flatbuffers_schema_to_cpp SRC_FBS)
434   compile_flatbuffers_schema_to_cpp_opt(${SRC_FBS} "--no-includes;--gen-compare")
435 endfunction()
436
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)
442   add_custom_command(
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"
447             -o "${SRC_FBS_DIR}"
448             "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}"
449     DEPENDS flatc
450     COMMENT "Run generation: '${GEN_BINARY_SCHEMA}'")
451   register_generated_output(${GEN_BINARY_SCHEMA})
452 endfunction()
453
454 function(compile_flatbuffers_schema_to_embedded_binary SRC_FBS OPT)
455   if(FLATBUFFERS_BUILD_LEGACY)
456     set(OPT ${OPT};--cpp-std c++0x)
457   else()
458     # --cpp-std is defined by flatc default settings.
459   endif()
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)
464   add_custom_command(
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
469           ${OPT}
470           --bfbs-comments --bfbs-builtins --bfbs-gen-embed
471           -I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test"
472           -o "${SRC_FBS_DIR}"
473           "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}"
474           DEPENDS flatc
475           COMMENT "Run generation: '${GEN_BFBS_HEADER}'")
476   register_generated_output(${GEN_BFBS_HEADER})
477 endfunction()
478
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}")
482
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.
496   endif()
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})
505   endif()
506
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)
516
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
527     )
528     if(FLATBUFFERS_CODE_SANITIZE)
529       add_fsanitize_to_target(flattests_cpp17 ${FLATBUFFERS_CODE_SANITIZE})
530     endif()
531   endif(FLATBUFFERS_BUILD_CPP17)
532 endif()
533
534 if(FLATBUFFERS_BUILD_GRPCTEST)
535   if(CMAKE_COMPILER_IS_GNUCXX)
536     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-parameter -Wno-shadow")
537   endif()
538   if(NOT GRPC_INSTALL_PATH)
539     message(SEND_ERROR "GRPC_INSTALL_PATH variable is not defined. See grpc/README.md")
540   endif()
541   if(NOT PROTOBUF_DOWNLOAD_PATH)
542     message(SEND_ERROR "PROTOBUF_DOWNLOAD_PATH variable is not defined. See grpc/README.md")
543   endif()
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})
556   endif()
557 endif()
558
559 include(CMake/Version.cmake)
560
561 if(FLATBUFFERS_INSTALL)
562   include(GNUInstallDirs)
563
564   install(DIRECTORY include/flatbuffers DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
565
566   set(FB_CMAKE_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/flatbuffers")
567
568   configure_file(CMake/FlatbuffersConfigVersion.cmake.in FlatbuffersConfigVersion.cmake @ONLY)
569   install(
570       FILES "CMake/FlatbuffersConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/FlatbuffersConfigVersion.cmake"
571       DESTINATION ${FB_CMAKE_DIR}
572   )
573
574   if(FLATBUFFERS_BUILD_FLATLIB)
575     if(CMAKE_VERSION VERSION_LESS 3.0)
576       install(
577         TARGETS flatbuffers EXPORT FlatbuffersTargets
578         ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
579       )
580     else()
581       install(
582         TARGETS flatbuffers EXPORT FlatbuffersTargets
583         ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
584         INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
585       )
586     endif()
587
588     install(EXPORT FlatbuffersTargets
589       FILE FlatbuffersTargets.cmake
590       NAMESPACE flatbuffers::
591       DESTINATION ${FB_CMAKE_DIR}
592     )
593   endif()
594
595   if(FLATBUFFERS_BUILD_FLATC)
596     install(
597       TARGETS flatc EXPORT FlatcTargets
598       RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
599     )
600
601     install(
602       EXPORT FlatcTargets
603       FILE FlatcTargets.cmake
604       NAMESPACE flatbuffers::
605       DESTINATION ${FB_CMAKE_DIR}
606     )
607   endif()
608
609   if(FLATBUFFERS_BUILD_SHAREDLIB)
610     if(CMAKE_VERSION VERSION_LESS 3.0)
611       install(
612         TARGETS flatbuffers_shared EXPORT FlatbuffersSharedTargets
613         ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
614         RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR}
615         LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
616       )
617     else()
618       install(
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}
624       )
625     endif()
626
627   install(
628       EXPORT FlatbuffersSharedTargets
629       FILE FlatbuffersSharedTargets.cmake
630       NAMESPACE flatbuffers::
631       DESTINATION ${FB_CMAKE_DIR}
632     )
633   endif()
634
635   if(FLATBUFFERS_BUILD_SHAREDLIB OR FLATBUFFERS_BUILD_FLATLIB)
636       configure_file(CMake/flatbuffers.pc.in flatbuffers.pc @ONLY)
637       install(
638         FILES "${CMAKE_CURRENT_BINARY_DIR}/flatbuffers.pc"
639         DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
640       )
641   endif()
642 endif()
643
644 if(FLATBUFFERS_BUILD_TESTS)
645   enable_testing()
646
647   add_test(NAME flattests COMMAND flattests)
648   if(FLATBUFFERS_BUILD_CPP17)
649     add_test(NAME flattests_cpp17 COMMAND flattests_cpp17)
650   endif()
651   if(FLATBUFFERS_BUILD_GRPCTEST)
652     add_test(NAME grpctest COMMAND grpctest)
653   endif()
654 endif()
655
656 # This target is sync-barrier.
657 # Other generate-dependent targets can depend on 'generated_code' only.
658 get_generated_output(fbs_generated)
659 if(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.")
664 endif()
665
666 include(CMake/BuildFlatBuffers.cmake)
667
668 if(UNIX)
669     # Use of CPack only supported on Linux systems.
670     if(FLATBUFFERS_PACKAGE_DEBIAN)
671         include(CMake/PackageDebian.cmake)
672         include(CPack)
673     endif()
674     if (FLATBUFFERS_PACKAGE_REDHAT)
675         include(CMake/PackageRedhat.cmake)
676         include(CPack)
677     endif()
678 endif()