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