blob: f5ae5ac81c794a5d2ef0b5329ec9111b8c3eb943 [file] [log] [blame]
James Kuszmaul8e62b022022-03-22 09:33:25 -07001# This is the legacy minimum version flatbuffers supported for a while.
2cmake_minimum_required(VERSION 2.8.12...3.22.1)
3
4# CMake version 3.16 is the 'de-facto' minimum version for flatbuffers. If the
5# current cmake is older than this, warn the user and include the legacy file to
6# provide some level of support.
7if(CMAKE_VERSION VERSION_LESS 3.16)
8 message(WARNING "Using cmake version ${CMAKE_VERSION} which is older than "
9 "our target version of 3.16. This will use the legacy CMakeLists.txt that "
10 "supports version 2.8.12 and higher, but not actively maintained. Consider "
11 "upgrading cmake to a newer version, as this may become a fatal error in the "
12 "future.")
13 # Use the legacy version of CMakeLists.txt
14 include(CMake/CMakeLists_legacy.cmake.in)
15 return()
16endif()
17
18# Attempt to read the current version of flatbuffers by looking at the latest tag.
19include(CMake/Version.cmake)
20
21if (POLICY CMP0048)
22 cmake_policy(SET CMP0048 NEW)
23 project(FlatBuffers
24 DESCRIPTION "Flatbuffers serialization library"
25 VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}
26 LANGUAGES CXX)
27else()
28 project(FlatBuffers)
29endif (POLICY CMP0048)
30
Austin Schuhe89fa2d2019-08-14 20:24:23 -070031# generate compile_commands.json
32set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
Austin Schuhe89fa2d2019-08-14 20:24:23 -070033
34# NOTE: Code coverage only works on Linux & OSX.
35option(FLATBUFFERS_CODE_COVERAGE "Enable the code coverage build option." OFF)
36option(FLATBUFFERS_BUILD_TESTS "Enable the build of tests and samples." ON)
37option(FLATBUFFERS_INSTALL "Enable the installation of targets." ON)
38option(FLATBUFFERS_BUILD_FLATLIB "Enable the build of the flatbuffers library"
39 ON)
40option(FLATBUFFERS_BUILD_FLATC "Enable the build of the flatbuffers compiler"
41 ON)
Austin Schuh272c6132020-11-14 16:37:52 -080042option(FLATBUFFERS_STATIC_FLATC "Build flatbuffers compiler with -static flag"
43 OFF)
Austin Schuhe89fa2d2019-08-14 20:24:23 -070044option(FLATBUFFERS_BUILD_FLATHASH "Enable the build of flathash" ON)
James Kuszmaul8e62b022022-03-22 09:33:25 -070045option(FLATBUFFERS_BUILD_BENCHMARKS "Enable the build of flatbenchmark."
46 OFF)
Austin Schuhe89fa2d2019-08-14 20:24:23 -070047option(FLATBUFFERS_BUILD_GRPCTEST "Enable the build of grpctest" OFF)
48option(FLATBUFFERS_BUILD_SHAREDLIB
49 "Enable the build of the flatbuffers shared library"
50 OFF)
51option(FLATBUFFERS_LIBCXX_WITH_CLANG "Force libc++ when using Clang" ON)
52# NOTE: Sanitizer check only works on Linux & OSX (gcc & llvm).
53option(FLATBUFFERS_CODE_SANITIZE
54 "Add '-fsanitize' flags to 'flattests' and 'flatc' targets."
55 OFF)
56option(FLATBUFFERS_PACKAGE_REDHAT
57 "Build an rpm using the 'package' target."
58 OFF)
59option(FLATBUFFERS_PACKAGE_DEBIAN
60 "Build an deb using the 'package' target."
61 OFF)
Austin Schuh272c6132020-11-14 16:37:52 -080062option(FLATBUFFERS_BUILD_CPP17
63 "Enable the build of c++17 test target. \"
64 Requirements: Clang6, GCC7, MSVC2017 (_MSC_VER >= 1914) or higher."
65 OFF)
66option(FLATBUFFERS_BUILD_LEGACY
67 "Run C++ code generator with '--cpp-std c++0x' switch."
68 OFF)
69option(FLATBUFFERS_ENABLE_PCH
70 "Enable precompile headers support for 'flatbuffers' and 'flatc'. \"
71 Only work if CMake supports 'target_precompile_headers'. \"
72 This can speed up compilation time."
73 OFF)
James Kuszmaul8e62b022022-03-22 09:33:25 -070074option(FLATBUFFERS_SKIP_MONSTER_EXTRA
75 "Skip generating monster_extra.fbs that contains non-supported numerical\"
76 types." OFF)
Austin Schuh2dd86a92022-09-14 21:19:23 -070077option(FLATBUFFERS_STRICT_MODE
78 "Build flatbuffers with all warnings as errors (-Werror or /WX)."
79 OFF)
80
81if(NOT DEFINED FLATBUFFERS_CPP_STD)
82 set(FLATBUFFERS_CPP_STD 11)
83endif()
84
85set(MSVC_LIKE OFF)
86if(MSVC OR CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC")
87 set(MSVC_LIKE ON)
88endif()
89
90if(DEFINED FLATBUFFERS_COMPILATION_TIMINGS)
91 message("Recording Compilation Timings to ${FLATBUFFERS_COMPILATION_TIMINGS}")
92 file(REMOVE ${FLATBUFFERS_COMPILATION_TIMINGS})
93 set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "time -f 'Wall: %E User: %U Sys: %S | %C' -q -a -o ${FLATBUFFERS_COMPILATION_TIMINGS}")
94 set_property(GLOBAL PROPERTY RULE_LAUNCH_CUSTOM "time -f 'Wall: %E User: %U Sys: %S | %C' -q -a -o ${FLATBUFFERS_COMPILATION_TIMINGS}")
95 set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK "time -f 'Wall: %E User: %U Sys: %S | %C' -q -a -o ${FLATBUFFERS_COMPILATION_TIMINGS}")
96endif()
Austin Schuhe89fa2d2019-08-14 20:24:23 -070097
98if(NOT FLATBUFFERS_BUILD_FLATC AND FLATBUFFERS_BUILD_TESTS)
99 message(WARNING
100 "Cannot build tests without building the compiler. Tests will be disabled.")
101 set(FLATBUFFERS_BUILD_TESTS OFF)
102endif()
103
104if(DEFINED FLATBUFFERS_MAX_PARSING_DEPTH)
105 # Override the default recursion depth limit.
106 add_definitions(-DFLATBUFFERS_MAX_PARSING_DEPTH=${FLATBUFFERS_MAX_PARSING_DEPTH})
107 message(STATUS "FLATBUFFERS_MAX_PARSING_DEPTH: ${FLATBUFFERS_MAX_PARSING_DEPTH}")
108endif()
109
110# Auto-detect locale-narrow 'strtod_l' and 'strtoull_l' functions.
111if(NOT DEFINED FLATBUFFERS_LOCALE_INDEPENDENT)
James Kuszmaul8e62b022022-03-22 09:33:25 -0700112 include(CheckCXXSymbolExists)
113
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700114 set(FLATBUFFERS_LOCALE_INDEPENDENT 0)
Austin Schuh2dd86a92022-09-14 21:19:23 -0700115 if(MSVC_LIKE)
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700116 check_cxx_symbol_exists(_strtof_l stdlib.h FLATBUFFERS_HAS_STRTOF_L)
117 check_cxx_symbol_exists(_strtoui64_l stdlib.h FLATBUFFERS_HAS_STRTOULL_L)
118 else()
119 check_cxx_symbol_exists(strtof_l stdlib.h FLATBUFFERS_HAS_STRTOF_L)
120 check_cxx_symbol_exists(strtoull_l stdlib.h FLATBUFFERS_HAS_STRTOULL_L)
121 endif()
122 if(FLATBUFFERS_HAS_STRTOF_L AND FLATBUFFERS_HAS_STRTOULL_L)
123 set(FLATBUFFERS_LOCALE_INDEPENDENT 1)
124 endif()
125endif()
126add_definitions(-DFLATBUFFERS_LOCALE_INDEPENDENT=$<BOOL:${FLATBUFFERS_LOCALE_INDEPENDENT}>)
127
128set(FlatBuffers_Library_SRCS
James Kuszmaul8e62b022022-03-22 09:33:25 -0700129 include/flatbuffers/allocator.h
130 include/flatbuffers/array.h
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700131 include/flatbuffers/base.h
James Kuszmaul8e62b022022-03-22 09:33:25 -0700132 include/flatbuffers/bfbs_generator.h
133 include/flatbuffers/buffer.h
134 include/flatbuffers/buffer_ref.h
135 include/flatbuffers/default_allocator.h
136 include/flatbuffers/detached_buffer.h
137 include/flatbuffers/flatbuffer_builder.h
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700138 include/flatbuffers/flatbuffers.h
James Kuszmaul8e62b022022-03-22 09:33:25 -0700139 include/flatbuffers/flexbuffers.h
Austin Schuh2dd86a92022-09-14 21:19:23 -0700140 include/flatbuffers/flex_flat_util.h
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700141 include/flatbuffers/hash.h
142 include/flatbuffers/idl.h
James Kuszmaul8e62b022022-03-22 09:33:25 -0700143 include/flatbuffers/minireflect.h
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700144 include/flatbuffers/reflection.h
145 include/flatbuffers/reflection_generated.h
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700146 include/flatbuffers/registry.h
James Kuszmaul8e62b022022-03-22 09:33:25 -0700147 include/flatbuffers/stl_emulation.h
148 include/flatbuffers/string.h
149 include/flatbuffers/struct.h
150 include/flatbuffers/table.h
151 include/flatbuffers/util.h
152 include/flatbuffers/vector.h
153 include/flatbuffers/vector_downward.h
154 include/flatbuffers/verifier.h
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700155 src/idl_parser.cpp
156 src/idl_gen_text.cpp
157 src/reflection.cpp
158 src/util.cpp
159)
160
161set(FlatBuffers_Compiler_SRCS
162 ${FlatBuffers_Library_SRCS}
163 src/idl_gen_cpp.cpp
Austin Schuh272c6132020-11-14 16:37:52 -0800164 src/idl_gen_csharp.cpp
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700165 src/idl_gen_dart.cpp
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700166 src/idl_gen_kotlin.cpp
167 src/idl_gen_go.cpp
Austin Schuh272c6132020-11-14 16:37:52 -0800168 src/idl_gen_java.cpp
James Kuszmaul8e62b022022-03-22 09:33:25 -0700169 src/idl_gen_ts.cpp
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700170 src/idl_gen_php.cpp
171 src/idl_gen_python.cpp
172 src/idl_gen_lobster.cpp
173 src/idl_gen_lua.cpp
174 src/idl_gen_rust.cpp
175 src/idl_gen_fbs.cpp
176 src/idl_gen_grpc.cpp
177 src/idl_gen_json_schema.cpp
Austin Schuh272c6132020-11-14 16:37:52 -0800178 src/idl_gen_swift.cpp
Austin Schuh2dd86a92022-09-14 21:19:23 -0700179 src/idl_namer.h
180 src/namer.h
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700181 src/flatc.cpp
182 src/flatc_main.cpp
James Kuszmaul8e62b022022-03-22 09:33:25 -0700183 src/bfbs_gen.h
184 src/bfbs_gen_lua.h
Austin Schuh2dd86a92022-09-14 21:19:23 -0700185 src/bfbs_namer.h
Austin Schuh272c6132020-11-14 16:37:52 -0800186 include/flatbuffers/code_generators.h
Austin Schuh2dd86a92022-09-14 21:19:23 -0700187 src/binary_annotator.h
188 src/binary_annotator.cpp
189 src/annotated_binary_text_gen.h
190 src/annotated_binary_text_gen.cpp
James Kuszmaul8e62b022022-03-22 09:33:25 -0700191 src/bfbs_gen_lua.cpp
Austin Schuh272c6132020-11-14 16:37:52 -0800192 src/code_generators.cpp
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700193 grpc/src/compiler/schema_interface.h
194 grpc/src/compiler/cpp_generator.h
195 grpc/src/compiler/cpp_generator.cc
196 grpc/src/compiler/go_generator.h
197 grpc/src/compiler/go_generator.cc
198 grpc/src/compiler/java_generator.h
199 grpc/src/compiler/java_generator.cc
Austin Schuh272c6132020-11-14 16:37:52 -0800200 grpc/src/compiler/python_generator.h
Austin Schuh272c6132020-11-14 16:37:52 -0800201 grpc/src/compiler/python_generator.cc
202 grpc/src/compiler/swift_generator.h
203 grpc/src/compiler/swift_generator.cc
204 grpc/src/compiler/ts_generator.h
205 grpc/src/compiler/ts_generator.cc
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700206)
207
208set(FlatHash_SRCS
209 include/flatbuffers/hash.h
210 src/flathash.cpp
211)
212
213set(FlatBuffers_Tests_SRCS
214 ${FlatBuffers_Library_SRCS}
215 src/idl_gen_fbs.cpp
Austin Schuh2dd86a92022-09-14 21:19:23 -0700216 tests/evolution_test.cpp
217 tests/flexbuffers_test.cpp
218 tests/fuzz_test.cpp
219 tests/json_test.cpp
220 tests/monster_test.cpp
221 tests/optional_scalars_test.cpp
222 tests/parser_test.cpp
223 tests/proto_test.cpp
224 tests/reflection_test.cpp
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700225 tests/test.cpp
226 tests/test_assert.h
227 tests/test_assert.cpp
228 tests/test_builder.h
229 tests/test_builder.cpp
Austin Schuh2dd86a92022-09-14 21:19:23 -0700230 tests/util_test.cpp
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700231 tests/native_type_test_impl.h
232 tests/native_type_test_impl.cpp
Austin Schuh272c6132020-11-14 16:37:52 -0800233 include/flatbuffers/code_generators.h
234 src/code_generators.cpp
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700235 # file generate by running compiler on tests/monster_test.fbs
236 ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h
Austin Schuh272c6132020-11-14 16:37:52 -0800237 # file generate by running compiler on namespace_test/namespace_test1.fbs
238 ${CMAKE_CURRENT_BINARY_DIR}/tests/namespace_test/namespace_test1_generated.h
239 ${CMAKE_CURRENT_BINARY_DIR}/tests/namespace_test/namespace_test2_generated.h
240 # file generate by running compiler on union_vector/union_vector.fbs
241 ${CMAKE_CURRENT_BINARY_DIR}/tests/union_vector/union_vector_generated.h
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700242 # file generate by running compiler on tests/arrays_test.fbs
243 ${CMAKE_CURRENT_BINARY_DIR}/tests/arrays_test_generated.h
244 # file generate by running compiler on tests/native_type_test.fbs
245 ${CMAKE_CURRENT_BINARY_DIR}/tests/native_type_test_generated.h
Austin Schuh272c6132020-11-14 16:37:52 -0800246 # file generate by running compiler on tests/monster_extra.fbs
247 ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_extra_generated.h
248 # file generate by running compiler on tests/monster_test.fbs
249 ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_bfbs_generated.h
250 # file generate by running compiler on tests/optional_scalars.fbs
251 ${CMAKE_CURRENT_BINARY_DIR}/tests/optional_scalars_generated.h
Austin Schuh2dd86a92022-09-14 21:19:23 -0700252 # file generate by running compiler on tests/native_inline_table_test.fbs
253 ${CMAKE_CURRENT_BINARY_DIR}/tests/native_inline_table_test_generated.h
Austin Schuh272c6132020-11-14 16:37:52 -0800254)
255
256set(FlatBuffers_Tests_CPP17_SRCS
257 ${FlatBuffers_Library_SRCS}
258 tests/test_assert.h
259 tests/test_assert.cpp
260 tests/cpp17/test_cpp17.cpp
261 # file generate by running compiler on tests/monster_test.fbs
262 ${CMAKE_CURRENT_BINARY_DIR}/tests/cpp17/generated_cpp17/monster_test_generated.h
263 ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h
264 ${CMAKE_CURRENT_BINARY_DIR}/tests/cpp17/generated_cpp17/optional_scalars_generated.h
265 ${CMAKE_CURRENT_BINARY_DIR}/tests/optional_scalars_generated.h
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700266)
267
268set(FlatBuffers_Sample_Binary_SRCS
269 include/flatbuffers/flatbuffers.h
270 samples/sample_binary.cpp
271 # file generated by running compiler on samples/monster.fbs
272 ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h
273)
274
275set(FlatBuffers_Sample_Text_SRCS
276 ${FlatBuffers_Library_SRCS}
277 samples/sample_text.cpp
278 # file generated by running compiler on samples/monster.fbs
279 ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h
280)
281
282set(FlatBuffers_Sample_BFBS_SRCS
283 ${FlatBuffers_Library_SRCS}
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700284 samples/sample_bfbs.cpp
285 # file generated by running compiler on samples/monster.fbs
286 ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h
287)
288
289set(FlatBuffers_GRPCTest_SRCS
290 include/flatbuffers/flatbuffers.h
291 include/flatbuffers/grpc.h
292 include/flatbuffers/util.h
293 src/util.cpp
294 tests/monster_test.grpc.fb.h
295 tests/test_assert.h
296 tests/test_builder.h
297 tests/monster_test.grpc.fb.cc
298 tests/test_assert.cpp
299 tests/test_builder.cpp
300 grpc/tests/grpctest.cpp
301 grpc/tests/message_builder_test.cpp
Austin Schuh272c6132020-11-14 16:37:52 -0800302 # file generate by running compiler on tests/monster_test.fbs
303 ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700304)
305
Austin Schuh2dd86a92022-09-14 21:19:23 -0700306# TODO(dbaileychess): Figure out how this would now work. I posted a question on
307# https://stackoverflow.com/questions/71772330/override-target-compile-options-via-cmake-command-line.
James Kuszmaul8e62b022022-03-22 09:33:25 -0700308# Append FLATBUFFERS_CXX_FLAGS to CMAKE_CXX_FLAGS.
309if(DEFINED FLATBUFFERS_CXX_FLAGS AND NOT EXISTS "${CMAKE_TOOLCHAIN_FILE}")
310 message(STATUS "extend CXX_FLAGS with ${FLATBUFFERS_CXX_FLAGS}")
311 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FLATBUFFERS_CXX_FLAGS}")
312endif()
313message(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
314
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700315function(add_fsanitize_to_target _target _sanitizer)
Austin Schuh272c6132020-11-14 16:37:52 -0800316 if(WIN32)
317 target_compile_definitions(${_target} PRIVATE FLATBUFFERS_MEMORY_LEAK_TRACKING)
318 message(STATUS "Sanitizer MSVC::_CrtDumpMemoryLeaks added to ${_target}")
319 else()
320 # FLATBUFFERS_CODE_SANITIZE: boolean {ON,OFF,YES,NO} or string with list of sanitizer.
321 # List of sanitizer is string starts with '=': "=address,undefined,thread,memory".
322 if((${CMAKE_CXX_COMPILER_ID} MATCHES "Clang") OR
323 ((${CMAKE_CXX_COMPILER_ID} MATCHES "GNU") AND NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.9"))
324 )
325 set(_sanitizer_flags "=address,undefined")
326 if(_sanitizer MATCHES "=.*")
327 # override default by user-defined sanitizer list
328 set(_sanitizer_flags ${_sanitizer})
329 endif()
330 target_compile_options(${_target} PRIVATE
331 -g -fsigned-char -fno-omit-frame-pointer
332 "-fsanitize${_sanitizer_flags}")
333 target_link_libraries(${_target} PRIVATE
334 "-fsanitize${_sanitizer_flags}")
335 set_property(TARGET ${_target} PROPERTY POSITION_INDEPENDENT_CODE ON)
336 message(STATUS "Sanitizer ${_sanitizer_flags} added to ${_target}")
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700337 endif()
Austin Schuh272c6132020-11-14 16:37:52 -0800338 endif()
339endfunction()
340
341function(add_pch_to_target _target _pch_header)
342 if(COMMAND target_precompile_headers)
343 target_precompile_headers(${_target} PRIVATE ${_pch_header})
344 if(NOT MSVC)
345 set_source_files_properties(src/util.cpp PROPERTIES SKIP_PRECOMPILE_HEADERS ON)
346 endif()
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700347 endif()
348endfunction()
349
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700350include_directories(include)
351include_directories(grpc)
352
Austin Schuh2dd86a92022-09-14 21:19:23 -0700353# Creates an interface library that stores the configuration settings that each
354# target links too. This is a compromise between setting configuration globally
355# with add_compile_options() and the more targetted target_compile_options().
356# This way each target in this file can share settings and override them if
357# needed.
358add_library(ProjectConfig INTERFACE)
359target_compile_features(ProjectConfig
360 INTERFACE
361 cxx_std_${FLATBUFFERS_CPP_STD}
362)
363
364# Force the standard to be met.
365set(CMAKE_CXX_STANDARD_REQUIRED ON)
366
367# We shouldn't rely on any compiler-extensions to make things work.
368set(CMAKE_CXX_EXTENSIONS OFF)
369
370if(MSVC_LIKE)
371 target_compile_options(ProjectConfig
372 INTERFACE
373 /W4
374 $<$<BOOL:${FLATBUFFERS_STRICT_MODE}>:
375 /WX # Treat all compiler warnings as errors
376 >
377 /wd4512 # C4512: assignment operator could not be generated
378 /wd4316 # C4316: object allocated on the heap may not be aligned
379 /wd4456 # C4456: hides previous local declaration
380 $<$<CXX_COMPILER_ID:Clang>:
381 /D_CRT_SECURE_NO_WARNINGS
382 >
383 )
384else()
385 if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
386 set(IS_CLANG ON)
387 else()
388 set(IS_CLANG OFF)
389 endif()
390 target_compile_options(ProjectConfig
391 INTERFACE
392 -Wall
393 $<$<BOOL:${FLATBUFFERS_STRICT_MODE}>:
394 -Werror # Treat all compiler warnings as errors
395
396 -fno-rtti # Disable runtime type information
397
398 $<$<CXX_COMPILER_ID:GNU>:
399 # False positive string overflow
400 # https://github.com/google/flatbuffers/issues/7366
401 -Wno-error=stringop-overflow
402 >
403 >
404 -pedantic
405 -Wextra
406 -Wno-unused-parameter
407 -Wold-style-cast
408 -fsigned-char
409 -Wnon-virtual-dtor
410
411 # This isn't working for some reason: $<$<CXX_COMPILER_ID:CLANG>:
412 $<$<BOOL:${IS_CLANG}>:
413 -Wno-unknown-warning-option
414 -Wmissing-declarations
415 -Wzero-as-null-pointer-constant
416 $<$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,3.8>:
417 -Wimplicit-fallthrough
418 -Wextra-semi
419 $<$<BOOL:${FLATBUFFERS_STRICT_MODE}>:
420 -Werror=unused-private-field
421 >
422 >
423 >
424
425 $<$<CXX_COMPILER_ID:GNU>:
426 $<$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,4.4>:
427 -Wunused-result
428 -Wunused-parameter
429 -Werror=unused-parameter
430 -Wmissing-declarations
431 >
432 $<$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,4.7>:
433 -Wzero-as-null-pointer-constant
434 >
435 $<$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,7.0>:
436 -faligned-new
437 $<$<BOOL:${FLATBUFFERS_STRICT_MODE}>:
438 -Werror=implicit-fallthrough=2
439 >
440 >
441 $<$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,8.0>:
442 -Wextra-semi
443 >
444 >
445
446 $<$<BOOL:${FLATBUFFERS_CODE_COVERAGE}>:
447 -g
448 -fprofile-arcs
449 -ftest-coverage
450 >
451 )
452
453 if(FLATBUFFERS_CODE_COVERAGE)
454 target_link_options(ProjectConfig
455 INTERFACE
456 -fprofile-arcs
457 -ftest-coverage
458 )
459 endif()
460endif()
461
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700462if(FLATBUFFERS_BUILD_FLATLIB)
463 add_library(flatbuffers STATIC ${FlatBuffers_Library_SRCS})
Austin Schuh2dd86a92022-09-14 21:19:23 -0700464
Austin Schuh272c6132020-11-14 16:37:52 -0800465 # Attach header directory for when build via add_subdirectory().
Austin Schuh2dd86a92022-09-14 21:19:23 -0700466 target_include_directories(flatbuffers
467 INTERFACE
468 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
469 )
470 target_link_libraries(flatbuffers PRIVATE $<BUILD_INTERFACE:ProjectConfig>)
471
Austin Schuh272c6132020-11-14 16:37:52 -0800472 if(FLATBUFFERS_ENABLE_PCH)
473 add_pch_to_target(flatbuffers include/flatbuffers/pch/pch.h)
474 endif()
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700475endif()
476
477if(FLATBUFFERS_BUILD_FLATC)
478 add_executable(flatc ${FlatBuffers_Compiler_SRCS})
Austin Schuh272c6132020-11-14 16:37:52 -0800479 if(FLATBUFFERS_ENABLE_PCH)
480 add_pch_to_target(flatc include/flatbuffers/pch/flatc_pch.h)
481 endif()
Austin Schuh2dd86a92022-09-14 21:19:23 -0700482
483 target_link_libraries(flatc PRIVATE $<BUILD_INTERFACE:ProjectConfig>)
484 target_compile_options(flatc
485 PUBLIC
486 $<$<AND:$<BOOL:${MSVC_LIKE}>,$<CONFIG:Release>>:
487 /MT
488 >
489 )
490
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700491 if(FLATBUFFERS_CODE_SANITIZE AND NOT WIN32)
492 add_fsanitize_to_target(flatc ${FLATBUFFERS_CODE_SANITIZE})
493 endif()
494 if(NOT FLATBUFFERS_FLATC_EXECUTABLE)
495 set(FLATBUFFERS_FLATC_EXECUTABLE $<TARGET_FILE:flatc>)
496 endif()
Austin Schuh272c6132020-11-14 16:37:52 -0800497 if(FLATBUFFERS_STATIC_FLATC AND NOT MSVC)
498 target_link_libraries(flatc PRIVATE -static)
499 endif()
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700500endif()
501
502if(FLATBUFFERS_BUILD_FLATHASH)
503 add_executable(flathash ${FlatHash_SRCS})
Austin Schuh2dd86a92022-09-14 21:19:23 -0700504 target_link_libraries(flathash PRIVATE $<BUILD_INTERFACE:ProjectConfig>)
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700505endif()
506
507if(FLATBUFFERS_BUILD_SHAREDLIB)
508 add_library(flatbuffers_shared SHARED ${FlatBuffers_Library_SRCS})
Austin Schuh2dd86a92022-09-14 21:19:23 -0700509 target_link_libraries(flatbuffers_shared PRIVATE $<BUILD_INTERFACE:ProjectConfig>)
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700510 # Shared object version: "major.minor.micro"
511 # - micro updated every release when there is no API/ABI changes
512 # - minor updated when there are additions in API/ABI
513 # - major (ABI number) updated when there are changes in ABI (or removals)
James Kuszmaul8e62b022022-03-22 09:33:25 -0700514 set(FlatBuffers_Library_SONAME_MAJOR ${VERSION_MAJOR})
515 set(FlatBuffers_Library_SONAME_FULL "${FlatBuffers_Library_SONAME_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700516 set_target_properties(flatbuffers_shared PROPERTIES OUTPUT_NAME flatbuffers
517 SOVERSION "${FlatBuffers_Library_SONAME_MAJOR}"
518 VERSION "${FlatBuffers_Library_SONAME_FULL}")
Austin Schuh272c6132020-11-14 16:37:52 -0800519 if(FLATBUFFERS_ENABLE_PCH)
520 add_pch_to_target(flatbuffers_shared include/flatbuffers/pch/pch.h)
521 endif()
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700522endif()
523
Austin Schuh272c6132020-11-14 16:37:52 -0800524# Global list of generated files.
525# Use the global property to be independent of PARENT_SCOPE.
526set_property(GLOBAL PROPERTY FBS_GENERATED_OUTPUTS)
527
528function(get_generated_output generated_files)
529 get_property(tmp GLOBAL PROPERTY FBS_GENERATED_OUTPUTS)
530 set(${generated_files} ${tmp} PARENT_SCOPE)
531endfunction(get_generated_output)
532
533function(register_generated_output file_name)
534 get_property(tmp GLOBAL PROPERTY FBS_GENERATED_OUTPUTS)
535 list(APPEND tmp ${file_name})
536 set_property(GLOBAL PROPERTY FBS_GENERATED_OUTPUTS ${tmp})
537endfunction(register_generated_output)
538
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700539function(compile_flatbuffers_schema_to_cpp_opt SRC_FBS OPT)
Austin Schuh272c6132020-11-14 16:37:52 -0800540 if(FLATBUFFERS_BUILD_LEGACY)
541 set(OPT ${OPT};--cpp-std c++0x)
542 else()
543 # --cpp-std is defined by flatc default settings.
544 endif()
545 message(STATUS "`${SRC_FBS}`: add generation of C++ code with '${OPT}'")
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700546 get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH)
547 string(REGEX REPLACE "\\.fbs$" "_generated.h" GEN_HEADER ${SRC_FBS})
548 add_custom_command(
549 OUTPUT ${GEN_HEADER}
Austin Schuh272c6132020-11-14 16:37:52 -0800550 COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}"
551 --cpp --gen-mutable --gen-object-api --reflect-names
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700552 --cpp-ptr-type flatbuffers::unique_ptr # Used to test with C++98 STLs
Austin Schuh272c6132020-11-14 16:37:52 -0800553 ${OPT}
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700554 -I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test"
Austin Schuh272c6132020-11-14 16:37:52 -0800555 -o "${SRC_FBS_DIR}"
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700556 "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}"
Austin Schuh272c6132020-11-14 16:37:52 -0800557 DEPENDS flatc
558 COMMENT "Run generation: '${GEN_HEADER}'")
559 register_generated_output(${GEN_HEADER})
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700560endfunction()
561
562function(compile_flatbuffers_schema_to_cpp SRC_FBS)
Austin Schuh272c6132020-11-14 16:37:52 -0800563 compile_flatbuffers_schema_to_cpp_opt(${SRC_FBS} "--no-includes;--gen-compare")
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700564endfunction()
565
566function(compile_flatbuffers_schema_to_binary SRC_FBS)
Austin Schuh272c6132020-11-14 16:37:52 -0800567 message(STATUS "`${SRC_FBS}`: add generation of binary (.bfbs) schema")
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700568 get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH)
569 string(REGEX REPLACE "\\.fbs$" ".bfbs" GEN_BINARY_SCHEMA ${SRC_FBS})
James Kuszmaul8e62b022022-03-22 09:33:25 -0700570 # For details about flags see generate_code.py
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700571 add_custom_command(
572 OUTPUT ${GEN_BINARY_SCHEMA}
Austin Schuh272c6132020-11-14 16:37:52 -0800573 COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}"
574 -b --schema --bfbs-comments --bfbs-builtins
James Kuszmaul8e62b022022-03-22 09:33:25 -0700575 --bfbs-filenames ${SRC_FBS_DIR}
Austin Schuh272c6132020-11-14 16:37:52 -0800576 -I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test"
577 -o "${SRC_FBS_DIR}"
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700578 "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}"
Austin Schuh272c6132020-11-14 16:37:52 -0800579 DEPENDS flatc
580 COMMENT "Run generation: '${GEN_BINARY_SCHEMA}'")
581 register_generated_output(${GEN_BINARY_SCHEMA})
582endfunction()
583
584function(compile_flatbuffers_schema_to_embedded_binary SRC_FBS OPT)
585 if(FLATBUFFERS_BUILD_LEGACY)
586 set(OPT ${OPT};--cpp-std c++0x)
587 else()
588 # --cpp-std is defined by flatc default settings.
589 endif()
590 message(STATUS "`${SRC_FBS}`: add generation of C++ embedded binary schema code with '${OPT}'")
591 get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH)
592 string(REGEX REPLACE "\\.fbs$" "_bfbs_generated.h" GEN_BFBS_HEADER ${SRC_FBS})
James Kuszmaul8e62b022022-03-22 09:33:25 -0700593 # For details about flags see generate_code.py
Austin Schuh272c6132020-11-14 16:37:52 -0800594 add_custom_command(
595 OUTPUT ${GEN_BFBS_HEADER}
596 COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}"
597 --cpp --gen-mutable --gen-object-api --reflect-names
598 --cpp-ptr-type flatbuffers::unique_ptr # Used to test with C++98 STLs
599 ${OPT}
600 --bfbs-comments --bfbs-builtins --bfbs-gen-embed
James Kuszmaul8e62b022022-03-22 09:33:25 -0700601 --bfbs-filenames ${SRC_FBS_DIR}
Austin Schuh272c6132020-11-14 16:37:52 -0800602 -I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test"
603 -o "${SRC_FBS_DIR}"
604 "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}"
605 DEPENDS flatc
606 COMMENT "Run generation: '${GEN_BFBS_HEADER}'")
607 register_generated_output(${GEN_BFBS_HEADER})
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700608endfunction()
609
610if(FLATBUFFERS_BUILD_TESTS)
Austin Schuh272c6132020-11-14 16:37:52 -0800611 file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/tests" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
612 file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/samples" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
613
614 # TODO Add (monster_test.fbs monsterdata_test.json)->monsterdata_test.mon
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700615 compile_flatbuffers_schema_to_cpp(tests/monster_test.fbs)
Austin Schuh272c6132020-11-14 16:37:52 -0800616 compile_flatbuffers_schema_to_binary(tests/monster_test.fbs)
James Kuszmaul8e62b022022-03-22 09:33:25 -0700617 compile_flatbuffers_schema_to_cpp_opt(tests/namespace_test/namespace_test1.fbs "--no-includes;--gen-compare;--gen-name-strings")
618 compile_flatbuffers_schema_to_cpp_opt(tests/namespace_test/namespace_test2.fbs "--no-includes;--gen-compare;--gen-name-strings")
619 compile_flatbuffers_schema_to_cpp_opt(tests/union_vector/union_vector.fbs "--no-includes;--gen-compare;--gen-name-strings")
Austin Schuh272c6132020-11-14 16:37:52 -0800620 compile_flatbuffers_schema_to_cpp(tests/optional_scalars.fbs)
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700621 compile_flatbuffers_schema_to_cpp_opt(tests/native_type_test.fbs "")
Austin Schuh272c6132020-11-14 16:37:52 -0800622 compile_flatbuffers_schema_to_cpp_opt(tests/arrays_test.fbs "--scoped-enums;--gen-compare")
623 compile_flatbuffers_schema_to_binary(tests/arrays_test.fbs)
624 compile_flatbuffers_schema_to_embedded_binary(tests/monster_test.fbs "--no-includes;--gen-compare")
Austin Schuh2dd86a92022-09-14 21:19:23 -0700625 compile_flatbuffers_schema_to_cpp(tests/native_inline_table_test.fbs "--gen-compare")
Austin Schuh272c6132020-11-14 16:37:52 -0800626 if(NOT (MSVC AND (MSVC_VERSION LESS 1900)))
627 compile_flatbuffers_schema_to_cpp(tests/monster_extra.fbs) # Test floating-point NAN/INF.
628 endif()
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700629 include_directories(${CMAKE_CURRENT_BINARY_DIR}/tests)
630 add_executable(flattests ${FlatBuffers_Tests_SRCS})
Austin Schuh2dd86a92022-09-14 21:19:23 -0700631 target_link_libraries(flattests PRIVATE $<BUILD_INTERFACE:ProjectConfig>)
632
Austin Schuh272c6132020-11-14 16:37:52 -0800633 add_dependencies(flattests generated_code)
Austin Schuh2dd86a92022-09-14 21:19:23 -0700634
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700635 if(FLATBUFFERS_CODE_SANITIZE)
Austin Schuh272c6132020-11-14 16:37:52 -0800636 add_fsanitize_to_target(flattests ${FLATBUFFERS_CODE_SANITIZE})
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700637 endif()
638
639 compile_flatbuffers_schema_to_cpp(samples/monster.fbs)
Austin Schuh272c6132020-11-14 16:37:52 -0800640 compile_flatbuffers_schema_to_binary(samples/monster.fbs)
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700641 include_directories(${CMAKE_CURRENT_BINARY_DIR}/samples)
Austin Schuh2dd86a92022-09-14 21:19:23 -0700642
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700643 add_executable(flatsamplebinary ${FlatBuffers_Sample_Binary_SRCS})
Austin Schuh2dd86a92022-09-14 21:19:23 -0700644 target_link_libraries(flatsamplebinary PRIVATE $<BUILD_INTERFACE:ProjectConfig>)
Austin Schuh272c6132020-11-14 16:37:52 -0800645 add_dependencies(flatsamplebinary generated_code)
Austin Schuh2dd86a92022-09-14 21:19:23 -0700646
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700647 add_executable(flatsampletext ${FlatBuffers_Sample_Text_SRCS})
Austin Schuh2dd86a92022-09-14 21:19:23 -0700648 target_link_libraries(flatsampletext PRIVATE $<BUILD_INTERFACE:ProjectConfig>)
Austin Schuh272c6132020-11-14 16:37:52 -0800649 add_dependencies(flatsampletext generated_code)
Austin Schuh2dd86a92022-09-14 21:19:23 -0700650
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700651 add_executable(flatsamplebfbs ${FlatBuffers_Sample_BFBS_SRCS})
Austin Schuh2dd86a92022-09-14 21:19:23 -0700652 target_link_libraries(flatsamplebfbs PRIVATE $<BUILD_INTERFACE:ProjectConfig>)
Austin Schuh272c6132020-11-14 16:37:52 -0800653 add_dependencies(flatsamplebfbs generated_code)
654
655 if(FLATBUFFERS_BUILD_CPP17)
656 # Don't generate header for flattests_cpp17 target.
657 # This target uses "generated_cpp17/monster_test_generated.h"
Austin Schuh272c6132020-11-14 16:37:52 -0800658 add_executable(flattests_cpp17 ${FlatBuffers_Tests_CPP17_SRCS})
659 add_dependencies(flattests_cpp17 generated_code)
Austin Schuh2dd86a92022-09-14 21:19:23 -0700660 target_link_libraries(flattests_cpp17 PRIVATE $<BUILD_INTERFACE:ProjectConfig>)
Austin Schuh272c6132020-11-14 16:37:52 -0800661 target_compile_features(flattests_cpp17 PRIVATE cxx_std_17)
Austin Schuh2dd86a92022-09-14 21:19:23 -0700662
Austin Schuh272c6132020-11-14 16:37:52 -0800663 if(FLATBUFFERS_CODE_SANITIZE)
664 add_fsanitize_to_target(flattests_cpp17 ${FLATBUFFERS_CODE_SANITIZE})
665 endif()
666 endif(FLATBUFFERS_BUILD_CPP17)
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700667endif()
668
669if(FLATBUFFERS_BUILD_GRPCTEST)
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700670 if(NOT GRPC_INSTALL_PATH)
671 message(SEND_ERROR "GRPC_INSTALL_PATH variable is not defined. See grpc/README.md")
672 endif()
673 if(NOT PROTOBUF_DOWNLOAD_PATH)
674 message(SEND_ERROR "PROTOBUF_DOWNLOAD_PATH variable is not defined. See grpc/README.md")
675 endif()
676 INCLUDE_DIRECTORIES(${GRPC_INSTALL_PATH}/include)
677 INCLUDE_DIRECTORIES(${PROTOBUF_DOWNLOAD_PATH}/src)
James Kuszmaul8e62b022022-03-22 09:33:25 -0700678 find_package(Threads REQUIRED)
679 list(APPEND CMAKE_PREFIX_PATH ${GRPC_INSTALL_PATH})
680 find_package(absl CONFIG REQUIRED)
681 find_package(protobuf CONFIG REQUIRED)
682 find_package(gRPC CONFIG REQUIRED)
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700683 add_executable(grpctest ${FlatBuffers_GRPCTest_SRCS})
Austin Schuh272c6132020-11-14 16:37:52 -0800684 add_dependencies(grpctest generated_code)
Austin Schuh2dd86a92022-09-14 21:19:23 -0700685 target_link_libraries(grpctext
686 PRIVATE
687 $<BUILD_INTERFACE:ProjectConfig>
688 gRPC::grpc++_unsecure
689 gRPC::gpr
690 pthread
691 dl
692 )
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700693endif()
694
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700695if(FLATBUFFERS_INSTALL)
696 include(GNUInstallDirs)
697
698 install(DIRECTORY include/flatbuffers DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
699
700 set(FB_CMAKE_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/flatbuffers")
701
Austin Schuh2dd86a92022-09-14 21:19:23 -0700702 configure_file(CMake/flatbuffers-config-version.cmake.in flatbuffers-config-version.cmake @ONLY)
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700703 install(
Austin Schuh2dd86a92022-09-14 21:19:23 -0700704 FILES
705 "CMake/flatbuffers-config.cmake"
706 "CMake/BuildFlatBuffers.cmake"
707 "${CMAKE_CURRENT_BINARY_DIR}/flatbuffers-config-version.cmake"
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700708 DESTINATION ${FB_CMAKE_DIR}
709 )
710
711 if(FLATBUFFERS_BUILD_FLATLIB)
James Kuszmaul8e62b022022-03-22 09:33:25 -0700712 install(
Austin Schuh2dd86a92022-09-14 21:19:23 -0700713 TARGETS flatbuffers EXPORT FlatBuffersTargets
James Kuszmaul8e62b022022-03-22 09:33:25 -0700714 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
715 INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
716 )
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700717
Austin Schuh2dd86a92022-09-14 21:19:23 -0700718 install(EXPORT FlatBuffersTargets
719 FILE FlatBuffersTargets.cmake
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700720 NAMESPACE flatbuffers::
721 DESTINATION ${FB_CMAKE_DIR}
722 )
723 endif()
724
725 if(FLATBUFFERS_BUILD_FLATC)
726 install(
727 TARGETS flatc EXPORT FlatcTargets
728 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
729 )
730
731 install(
732 EXPORT FlatcTargets
733 FILE FlatcTargets.cmake
734 NAMESPACE flatbuffers::
735 DESTINATION ${FB_CMAKE_DIR}
736 )
737 endif()
738
739 if(FLATBUFFERS_BUILD_SHAREDLIB)
James Kuszmaul8e62b022022-03-22 09:33:25 -0700740 install(
Austin Schuh2dd86a92022-09-14 21:19:23 -0700741 TARGETS flatbuffers_shared EXPORT FlatBuffersSharedTargets
James Kuszmaul8e62b022022-03-22 09:33:25 -0700742 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
743 RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR}
744 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
745 INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
746 )
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700747
James Kuszmaul8e62b022022-03-22 09:33:25 -0700748 install(
Austin Schuh2dd86a92022-09-14 21:19:23 -0700749 EXPORT FlatBuffersSharedTargets
750 FILE FlatBuffersSharedTargets.cmake
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700751 NAMESPACE flatbuffers::
752 DESTINATION ${FB_CMAKE_DIR}
753 )
754 endif()
James Kuszmaul8e62b022022-03-22 09:33:25 -0700755
756 if(FLATBUFFERS_BUILD_SHAREDLIB OR FLATBUFFERS_BUILD_FLATLIB)
757 configure_file(CMake/flatbuffers.pc.in flatbuffers.pc @ONLY)
758 install(
759 FILES "${CMAKE_CURRENT_BINARY_DIR}/flatbuffers.pc"
760 DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
761 )
762 endif()
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700763endif()
764
765if(FLATBUFFERS_BUILD_TESTS)
766 enable_testing()
767
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700768 add_test(NAME flattests COMMAND flattests)
Austin Schuh272c6132020-11-14 16:37:52 -0800769 if(FLATBUFFERS_BUILD_CPP17)
770 add_test(NAME flattests_cpp17 COMMAND flattests_cpp17)
771 endif()
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700772 if(FLATBUFFERS_BUILD_GRPCTEST)
773 add_test(NAME grpctest COMMAND grpctest)
774 endif()
775endif()
776
Austin Schuh272c6132020-11-14 16:37:52 -0800777# This target is sync-barrier.
778# Other generate-dependent targets can depend on 'generated_code' only.
779get_generated_output(fbs_generated)
780if(fbs_generated)
781 # message(STATUS "Add generated_code target with files:${fbs_generated}")
782 add_custom_target(generated_code
783 DEPENDS ${fbs_generated}
784 COMMENT "All generated files were updated.")
785endif()
786
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700787include(CMake/BuildFlatBuffers.cmake)
788
789if(UNIX)
790 # Use of CPack only supported on Linux systems.
791 if(FLATBUFFERS_PACKAGE_DEBIAN)
792 include(CMake/PackageDebian.cmake)
Austin Schuh272c6132020-11-14 16:37:52 -0800793 include(CPack)
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700794 endif()
795 if (FLATBUFFERS_PACKAGE_REDHAT)
796 include(CMake/PackageRedhat.cmake)
Austin Schuh272c6132020-11-14 16:37:52 -0800797 include(CPack)
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700798 endif()
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700799endif()
James Kuszmaul8e62b022022-03-22 09:33:25 -0700800
801# Include for running Google Benchmarks.
802if(FLATBUFFERS_BUILD_BENCHMARKS)
803 add_subdirectory(benchmarks)
804endif()
805
806# Add FlatBuffers::FlatBuffers interface, needed for FetchContent_Declare
807add_library(FlatBuffers INTERFACE)
808add_library(FlatBuffers::FlatBuffers ALIAS FlatBuffers)
809target_include_directories(
810 FlatBuffers
811 INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
Austin Schuh2dd86a92022-09-14 21:19:23 -0700812 $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/include>)