blob: 395ac59e7c338bcb8f1c7a34c6ff6da411bc5dac [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
James Kuszmaul3b15b0c2022-11-08 14:03:16 -0800128if(NOT WIN32)
129 check_symbol_exists(realpath "stdlib.h" HAVE_REALPATH)
130 if(NOT HAVE_REALPATH)
131 add_definitions(-DFLATBUFFERS_NO_ABSOLUTE_PATH_RESOLUTION)
132 endif()
133endif()
134
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700135set(FlatBuffers_Library_SRCS
James Kuszmaul8e62b022022-03-22 09:33:25 -0700136 include/flatbuffers/allocator.h
137 include/flatbuffers/array.h
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700138 include/flatbuffers/base.h
James Kuszmaul8e62b022022-03-22 09:33:25 -0700139 include/flatbuffers/bfbs_generator.h
140 include/flatbuffers/buffer.h
141 include/flatbuffers/buffer_ref.h
142 include/flatbuffers/default_allocator.h
143 include/flatbuffers/detached_buffer.h
144 include/flatbuffers/flatbuffer_builder.h
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700145 include/flatbuffers/flatbuffers.h
James Kuszmaul8e62b022022-03-22 09:33:25 -0700146 include/flatbuffers/flexbuffers.h
Austin Schuh2dd86a92022-09-14 21:19:23 -0700147 include/flatbuffers/flex_flat_util.h
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700148 include/flatbuffers/hash.h
149 include/flatbuffers/idl.h
James Kuszmaul8e62b022022-03-22 09:33:25 -0700150 include/flatbuffers/minireflect.h
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700151 include/flatbuffers/reflection.h
152 include/flatbuffers/reflection_generated.h
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700153 include/flatbuffers/registry.h
James Kuszmaul8e62b022022-03-22 09:33:25 -0700154 include/flatbuffers/stl_emulation.h
155 include/flatbuffers/string.h
156 include/flatbuffers/struct.h
157 include/flatbuffers/table.h
158 include/flatbuffers/util.h
159 include/flatbuffers/vector.h
160 include/flatbuffers/vector_downward.h
161 include/flatbuffers/verifier.h
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700162 src/idl_parser.cpp
163 src/idl_gen_text.cpp
164 src/reflection.cpp
165 src/util.cpp
166)
167
168set(FlatBuffers_Compiler_SRCS
169 ${FlatBuffers_Library_SRCS}
170 src/idl_gen_cpp.cpp
Austin Schuh272c6132020-11-14 16:37:52 -0800171 src/idl_gen_csharp.cpp
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700172 src/idl_gen_dart.cpp
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700173 src/idl_gen_kotlin.cpp
174 src/idl_gen_go.cpp
Austin Schuh272c6132020-11-14 16:37:52 -0800175 src/idl_gen_java.cpp
James Kuszmaul8e62b022022-03-22 09:33:25 -0700176 src/idl_gen_ts.cpp
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700177 src/idl_gen_php.cpp
178 src/idl_gen_python.cpp
179 src/idl_gen_lobster.cpp
180 src/idl_gen_lua.cpp
181 src/idl_gen_rust.cpp
182 src/idl_gen_fbs.cpp
183 src/idl_gen_grpc.cpp
184 src/idl_gen_json_schema.cpp
Austin Schuh272c6132020-11-14 16:37:52 -0800185 src/idl_gen_swift.cpp
Austin Schuh2dd86a92022-09-14 21:19:23 -0700186 src/idl_namer.h
187 src/namer.h
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700188 src/flatc.cpp
189 src/flatc_main.cpp
James Kuszmaul8e62b022022-03-22 09:33:25 -0700190 src/bfbs_gen.h
191 src/bfbs_gen_lua.h
James Kuszmaul3b15b0c2022-11-08 14:03:16 -0800192 src/bfbs_gen_nim.h
Austin Schuh2dd86a92022-09-14 21:19:23 -0700193 src/bfbs_namer.h
Austin Schuh272c6132020-11-14 16:37:52 -0800194 include/flatbuffers/code_generators.h
Austin Schuh2dd86a92022-09-14 21:19:23 -0700195 src/binary_annotator.h
196 src/binary_annotator.cpp
197 src/annotated_binary_text_gen.h
198 src/annotated_binary_text_gen.cpp
James Kuszmaul8e62b022022-03-22 09:33:25 -0700199 src/bfbs_gen_lua.cpp
James Kuszmaul3b15b0c2022-11-08 14:03:16 -0800200 src/bfbs_gen_nim.cpp
Austin Schuh272c6132020-11-14 16:37:52 -0800201 src/code_generators.cpp
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700202 grpc/src/compiler/schema_interface.h
203 grpc/src/compiler/cpp_generator.h
204 grpc/src/compiler/cpp_generator.cc
205 grpc/src/compiler/go_generator.h
206 grpc/src/compiler/go_generator.cc
207 grpc/src/compiler/java_generator.h
208 grpc/src/compiler/java_generator.cc
Austin Schuh272c6132020-11-14 16:37:52 -0800209 grpc/src/compiler/python_generator.h
Austin Schuh272c6132020-11-14 16:37:52 -0800210 grpc/src/compiler/python_generator.cc
211 grpc/src/compiler/swift_generator.h
212 grpc/src/compiler/swift_generator.cc
213 grpc/src/compiler/ts_generator.h
214 grpc/src/compiler/ts_generator.cc
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700215)
216
217set(FlatHash_SRCS
218 include/flatbuffers/hash.h
219 src/flathash.cpp
220)
221
222set(FlatBuffers_Tests_SRCS
223 ${FlatBuffers_Library_SRCS}
224 src/idl_gen_fbs.cpp
Austin Schuh2dd86a92022-09-14 21:19:23 -0700225 tests/evolution_test.cpp
226 tests/flexbuffers_test.cpp
227 tests/fuzz_test.cpp
228 tests/json_test.cpp
229 tests/monster_test.cpp
230 tests/optional_scalars_test.cpp
231 tests/parser_test.cpp
232 tests/proto_test.cpp
233 tests/reflection_test.cpp
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700234 tests/test.cpp
235 tests/test_assert.h
236 tests/test_assert.cpp
237 tests/test_builder.h
238 tests/test_builder.cpp
Austin Schuh2dd86a92022-09-14 21:19:23 -0700239 tests/util_test.cpp
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700240 tests/native_type_test_impl.h
241 tests/native_type_test_impl.cpp
James Kuszmaul3b15b0c2022-11-08 14:03:16 -0800242 tests/alignment_test.h
243 tests/alignment_test.cpp
Austin Schuh272c6132020-11-14 16:37:52 -0800244 include/flatbuffers/code_generators.h
245 src/code_generators.cpp
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700246 # file generate by running compiler on tests/monster_test.fbs
247 ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h
Austin Schuh272c6132020-11-14 16:37:52 -0800248 # file generate by running compiler on namespace_test/namespace_test1.fbs
249 ${CMAKE_CURRENT_BINARY_DIR}/tests/namespace_test/namespace_test1_generated.h
250 ${CMAKE_CURRENT_BINARY_DIR}/tests/namespace_test/namespace_test2_generated.h
251 # file generate by running compiler on union_vector/union_vector.fbs
252 ${CMAKE_CURRENT_BINARY_DIR}/tests/union_vector/union_vector_generated.h
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700253 # file generate by running compiler on tests/arrays_test.fbs
254 ${CMAKE_CURRENT_BINARY_DIR}/tests/arrays_test_generated.h
255 # file generate by running compiler on tests/native_type_test.fbs
256 ${CMAKE_CURRENT_BINARY_DIR}/tests/native_type_test_generated.h
Austin Schuh272c6132020-11-14 16:37:52 -0800257 # file generate by running compiler on tests/monster_extra.fbs
258 ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_extra_generated.h
259 # file generate by running compiler on tests/monster_test.fbs
260 ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_bfbs_generated.h
261 # file generate by running compiler on tests/optional_scalars.fbs
262 ${CMAKE_CURRENT_BINARY_DIR}/tests/optional_scalars_generated.h
Austin Schuh2dd86a92022-09-14 21:19:23 -0700263 # file generate by running compiler on tests/native_inline_table_test.fbs
264 ${CMAKE_CURRENT_BINARY_DIR}/tests/native_inline_table_test_generated.h
James Kuszmaul3b15b0c2022-11-08 14:03:16 -0800265 # file generate by running compiler on tests/alignment_test.fbs
266 ${CMAKE_CURRENT_BINARY_DIR}/tests/alignment_test_generated.h
Austin Schuh272c6132020-11-14 16:37:52 -0800267)
268
269set(FlatBuffers_Tests_CPP17_SRCS
270 ${FlatBuffers_Library_SRCS}
271 tests/test_assert.h
272 tests/test_assert.cpp
273 tests/cpp17/test_cpp17.cpp
274 # file generate by running compiler on tests/monster_test.fbs
275 ${CMAKE_CURRENT_BINARY_DIR}/tests/cpp17/generated_cpp17/monster_test_generated.h
276 ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h
277 ${CMAKE_CURRENT_BINARY_DIR}/tests/cpp17/generated_cpp17/optional_scalars_generated.h
278 ${CMAKE_CURRENT_BINARY_DIR}/tests/optional_scalars_generated.h
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700279)
280
281set(FlatBuffers_Sample_Binary_SRCS
282 include/flatbuffers/flatbuffers.h
283 samples/sample_binary.cpp
284 # file generated by running compiler on samples/monster.fbs
285 ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h
286)
287
288set(FlatBuffers_Sample_Text_SRCS
289 ${FlatBuffers_Library_SRCS}
290 samples/sample_text.cpp
291 # file generated by running compiler on samples/monster.fbs
292 ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h
293)
294
295set(FlatBuffers_Sample_BFBS_SRCS
296 ${FlatBuffers_Library_SRCS}
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700297 samples/sample_bfbs.cpp
298 # file generated by running compiler on samples/monster.fbs
299 ${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h
300)
301
302set(FlatBuffers_GRPCTest_SRCS
303 include/flatbuffers/flatbuffers.h
304 include/flatbuffers/grpc.h
305 include/flatbuffers/util.h
306 src/util.cpp
307 tests/monster_test.grpc.fb.h
308 tests/test_assert.h
309 tests/test_builder.h
310 tests/monster_test.grpc.fb.cc
311 tests/test_assert.cpp
312 tests/test_builder.cpp
313 grpc/tests/grpctest.cpp
314 grpc/tests/message_builder_test.cpp
Austin Schuh272c6132020-11-14 16:37:52 -0800315 # file generate by running compiler on tests/monster_test.fbs
316 ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700317)
318
Austin Schuh2dd86a92022-09-14 21:19:23 -0700319# TODO(dbaileychess): Figure out how this would now work. I posted a question on
320# https://stackoverflow.com/questions/71772330/override-target-compile-options-via-cmake-command-line.
James Kuszmaul8e62b022022-03-22 09:33:25 -0700321# Append FLATBUFFERS_CXX_FLAGS to CMAKE_CXX_FLAGS.
322if(DEFINED FLATBUFFERS_CXX_FLAGS AND NOT EXISTS "${CMAKE_TOOLCHAIN_FILE}")
323 message(STATUS "extend CXX_FLAGS with ${FLATBUFFERS_CXX_FLAGS}")
324 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FLATBUFFERS_CXX_FLAGS}")
325endif()
326message(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
327
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700328function(add_fsanitize_to_target _target _sanitizer)
Austin Schuh272c6132020-11-14 16:37:52 -0800329 if(WIN32)
330 target_compile_definitions(${_target} PRIVATE FLATBUFFERS_MEMORY_LEAK_TRACKING)
331 message(STATUS "Sanitizer MSVC::_CrtDumpMemoryLeaks added to ${_target}")
332 else()
333 # FLATBUFFERS_CODE_SANITIZE: boolean {ON,OFF,YES,NO} or string with list of sanitizer.
334 # List of sanitizer is string starts with '=': "=address,undefined,thread,memory".
335 if((${CMAKE_CXX_COMPILER_ID} MATCHES "Clang") OR
336 ((${CMAKE_CXX_COMPILER_ID} MATCHES "GNU") AND NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.9"))
337 )
338 set(_sanitizer_flags "=address,undefined")
339 if(_sanitizer MATCHES "=.*")
340 # override default by user-defined sanitizer list
341 set(_sanitizer_flags ${_sanitizer})
342 endif()
343 target_compile_options(${_target} PRIVATE
344 -g -fsigned-char -fno-omit-frame-pointer
345 "-fsanitize${_sanitizer_flags}")
346 target_link_libraries(${_target} PRIVATE
347 "-fsanitize${_sanitizer_flags}")
348 set_property(TARGET ${_target} PROPERTY POSITION_INDEPENDENT_CODE ON)
349 message(STATUS "Sanitizer ${_sanitizer_flags} added to ${_target}")
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700350 endif()
Austin Schuh272c6132020-11-14 16:37:52 -0800351 endif()
352endfunction()
353
354function(add_pch_to_target _target _pch_header)
355 if(COMMAND target_precompile_headers)
356 target_precompile_headers(${_target} PRIVATE ${_pch_header})
357 if(NOT MSVC)
358 set_source_files_properties(src/util.cpp PROPERTIES SKIP_PRECOMPILE_HEADERS ON)
359 endif()
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700360 endif()
361endfunction()
362
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700363include_directories(include)
364include_directories(grpc)
365
Austin Schuh2dd86a92022-09-14 21:19:23 -0700366# Creates an interface library that stores the configuration settings that each
367# target links too. This is a compromise between setting configuration globally
368# with add_compile_options() and the more targetted target_compile_options().
369# This way each target in this file can share settings and override them if
370# needed.
371add_library(ProjectConfig INTERFACE)
372target_compile_features(ProjectConfig
373 INTERFACE
374 cxx_std_${FLATBUFFERS_CPP_STD}
375)
376
377# Force the standard to be met.
378set(CMAKE_CXX_STANDARD_REQUIRED ON)
379
380# We shouldn't rely on any compiler-extensions to make things work.
381set(CMAKE_CXX_EXTENSIONS OFF)
382
383if(MSVC_LIKE)
384 target_compile_options(ProjectConfig
385 INTERFACE
386 /W4
387 $<$<BOOL:${FLATBUFFERS_STRICT_MODE}>:
388 /WX # Treat all compiler warnings as errors
389 >
390 /wd4512 # C4512: assignment operator could not be generated
391 /wd4316 # C4316: object allocated on the heap may not be aligned
392 /wd4456 # C4456: hides previous local declaration
393 $<$<CXX_COMPILER_ID:Clang>:
394 /D_CRT_SECURE_NO_WARNINGS
395 >
396 )
397else()
398 if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
399 set(IS_CLANG ON)
400 else()
401 set(IS_CLANG OFF)
402 endif()
403 target_compile_options(ProjectConfig
404 INTERFACE
405 -Wall
406 $<$<BOOL:${FLATBUFFERS_STRICT_MODE}>:
407 -Werror # Treat all compiler warnings as errors
408
409 -fno-rtti # Disable runtime type information
410
411 $<$<CXX_COMPILER_ID:GNU>:
412 # False positive string overflow
413 # https://github.com/google/flatbuffers/issues/7366
414 -Wno-error=stringop-overflow
415 >
416 >
417 -pedantic
418 -Wextra
419 -Wno-unused-parameter
420 -Wold-style-cast
421 -fsigned-char
422 -Wnon-virtual-dtor
423
424 # This isn't working for some reason: $<$<CXX_COMPILER_ID:CLANG>:
425 $<$<BOOL:${IS_CLANG}>:
426 -Wno-unknown-warning-option
427 -Wmissing-declarations
428 -Wzero-as-null-pointer-constant
429 $<$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,3.8>:
430 -Wimplicit-fallthrough
431 -Wextra-semi
432 $<$<BOOL:${FLATBUFFERS_STRICT_MODE}>:
433 -Werror=unused-private-field
434 >
435 >
436 >
437
438 $<$<CXX_COMPILER_ID:GNU>:
439 $<$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,4.4>:
440 -Wunused-result
441 -Wunused-parameter
442 -Werror=unused-parameter
443 -Wmissing-declarations
444 >
445 $<$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,4.7>:
446 -Wzero-as-null-pointer-constant
447 >
448 $<$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,7.0>:
449 -faligned-new
450 $<$<BOOL:${FLATBUFFERS_STRICT_MODE}>:
451 -Werror=implicit-fallthrough=2
452 >
453 >
454 $<$<VERSION_GREATER:$<CXX_COMPILER_VERSION>,8.0>:
455 -Wextra-semi
456 >
457 >
458
459 $<$<BOOL:${FLATBUFFERS_CODE_COVERAGE}>:
460 -g
461 -fprofile-arcs
462 -ftest-coverage
463 >
464 )
465
466 if(FLATBUFFERS_CODE_COVERAGE)
467 target_link_options(ProjectConfig
468 INTERFACE
469 -fprofile-arcs
470 -ftest-coverage
471 )
472 endif()
473endif()
474
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700475if(FLATBUFFERS_BUILD_FLATLIB)
476 add_library(flatbuffers STATIC ${FlatBuffers_Library_SRCS})
Austin Schuh2dd86a92022-09-14 21:19:23 -0700477
Austin Schuh272c6132020-11-14 16:37:52 -0800478 # Attach header directory for when build via add_subdirectory().
Austin Schuh2dd86a92022-09-14 21:19:23 -0700479 target_include_directories(flatbuffers
480 INTERFACE
481 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
482 )
483 target_link_libraries(flatbuffers PRIVATE $<BUILD_INTERFACE:ProjectConfig>)
484
Austin Schuh272c6132020-11-14 16:37:52 -0800485 if(FLATBUFFERS_ENABLE_PCH)
486 add_pch_to_target(flatbuffers include/flatbuffers/pch/pch.h)
487 endif()
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700488endif()
489
490if(FLATBUFFERS_BUILD_FLATC)
491 add_executable(flatc ${FlatBuffers_Compiler_SRCS})
Austin Schuh272c6132020-11-14 16:37:52 -0800492 if(FLATBUFFERS_ENABLE_PCH)
493 add_pch_to_target(flatc include/flatbuffers/pch/flatc_pch.h)
494 endif()
Austin Schuh2dd86a92022-09-14 21:19:23 -0700495
496 target_link_libraries(flatc PRIVATE $<BUILD_INTERFACE:ProjectConfig>)
497 target_compile_options(flatc
498 PUBLIC
499 $<$<AND:$<BOOL:${MSVC_LIKE}>,$<CONFIG:Release>>:
500 /MT
501 >
502 )
503
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700504 if(FLATBUFFERS_CODE_SANITIZE AND NOT WIN32)
505 add_fsanitize_to_target(flatc ${FLATBUFFERS_CODE_SANITIZE})
506 endif()
507 if(NOT FLATBUFFERS_FLATC_EXECUTABLE)
508 set(FLATBUFFERS_FLATC_EXECUTABLE $<TARGET_FILE:flatc>)
509 endif()
Austin Schuh272c6132020-11-14 16:37:52 -0800510 if(FLATBUFFERS_STATIC_FLATC AND NOT MSVC)
511 target_link_libraries(flatc PRIVATE -static)
512 endif()
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700513endif()
514
515if(FLATBUFFERS_BUILD_FLATHASH)
516 add_executable(flathash ${FlatHash_SRCS})
Austin Schuh2dd86a92022-09-14 21:19:23 -0700517 target_link_libraries(flathash PRIVATE $<BUILD_INTERFACE:ProjectConfig>)
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700518endif()
519
520if(FLATBUFFERS_BUILD_SHAREDLIB)
521 add_library(flatbuffers_shared SHARED ${FlatBuffers_Library_SRCS})
Austin Schuh2dd86a92022-09-14 21:19:23 -0700522 target_link_libraries(flatbuffers_shared PRIVATE $<BUILD_INTERFACE:ProjectConfig>)
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700523 # Shared object version: "major.minor.micro"
524 # - micro updated every release when there is no API/ABI changes
525 # - minor updated when there are additions in API/ABI
526 # - major (ABI number) updated when there are changes in ABI (or removals)
James Kuszmaul8e62b022022-03-22 09:33:25 -0700527 set(FlatBuffers_Library_SONAME_MAJOR ${VERSION_MAJOR})
528 set(FlatBuffers_Library_SONAME_FULL "${FlatBuffers_Library_SONAME_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700529 set_target_properties(flatbuffers_shared PROPERTIES OUTPUT_NAME flatbuffers
530 SOVERSION "${FlatBuffers_Library_SONAME_MAJOR}"
531 VERSION "${FlatBuffers_Library_SONAME_FULL}")
Austin Schuh272c6132020-11-14 16:37:52 -0800532 if(FLATBUFFERS_ENABLE_PCH)
533 add_pch_to_target(flatbuffers_shared include/flatbuffers/pch/pch.h)
534 endif()
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700535endif()
536
Austin Schuh272c6132020-11-14 16:37:52 -0800537# Global list of generated files.
538# Use the global property to be independent of PARENT_SCOPE.
539set_property(GLOBAL PROPERTY FBS_GENERATED_OUTPUTS)
540
541function(get_generated_output generated_files)
542 get_property(tmp GLOBAL PROPERTY FBS_GENERATED_OUTPUTS)
543 set(${generated_files} ${tmp} PARENT_SCOPE)
544endfunction(get_generated_output)
545
546function(register_generated_output file_name)
547 get_property(tmp GLOBAL PROPERTY FBS_GENERATED_OUTPUTS)
548 list(APPEND tmp ${file_name})
549 set_property(GLOBAL PROPERTY FBS_GENERATED_OUTPUTS ${tmp})
550endfunction(register_generated_output)
551
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700552function(compile_flatbuffers_schema_to_cpp_opt SRC_FBS OPT)
Austin Schuh272c6132020-11-14 16:37:52 -0800553 if(FLATBUFFERS_BUILD_LEGACY)
554 set(OPT ${OPT};--cpp-std c++0x)
555 else()
556 # --cpp-std is defined by flatc default settings.
557 endif()
558 message(STATUS "`${SRC_FBS}`: add generation of C++ code with '${OPT}'")
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700559 get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH)
560 string(REGEX REPLACE "\\.fbs$" "_generated.h" GEN_HEADER ${SRC_FBS})
561 add_custom_command(
562 OUTPUT ${GEN_HEADER}
Austin Schuh272c6132020-11-14 16:37:52 -0800563 COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}"
564 --cpp --gen-mutable --gen-object-api --reflect-names
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700565 --cpp-ptr-type flatbuffers::unique_ptr # Used to test with C++98 STLs
Austin Schuh272c6132020-11-14 16:37:52 -0800566 ${OPT}
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700567 -I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test"
Austin Schuh272c6132020-11-14 16:37:52 -0800568 -o "${SRC_FBS_DIR}"
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700569 "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}"
Austin Schuh272c6132020-11-14 16:37:52 -0800570 DEPENDS flatc
571 COMMENT "Run generation: '${GEN_HEADER}'")
572 register_generated_output(${GEN_HEADER})
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700573endfunction()
574
575function(compile_flatbuffers_schema_to_cpp SRC_FBS)
Austin Schuh272c6132020-11-14 16:37:52 -0800576 compile_flatbuffers_schema_to_cpp_opt(${SRC_FBS} "--no-includes;--gen-compare")
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700577endfunction()
578
579function(compile_flatbuffers_schema_to_binary SRC_FBS)
Austin Schuh272c6132020-11-14 16:37:52 -0800580 message(STATUS "`${SRC_FBS}`: add generation of binary (.bfbs) schema")
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700581 get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH)
582 string(REGEX REPLACE "\\.fbs$" ".bfbs" GEN_BINARY_SCHEMA ${SRC_FBS})
James Kuszmaul8e62b022022-03-22 09:33:25 -0700583 # For details about flags see generate_code.py
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700584 add_custom_command(
585 OUTPUT ${GEN_BINARY_SCHEMA}
Austin Schuh272c6132020-11-14 16:37:52 -0800586 COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}"
587 -b --schema --bfbs-comments --bfbs-builtins
James Kuszmaul8e62b022022-03-22 09:33:25 -0700588 --bfbs-filenames ${SRC_FBS_DIR}
Austin Schuh272c6132020-11-14 16:37:52 -0800589 -I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test"
590 -o "${SRC_FBS_DIR}"
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700591 "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}"
Austin Schuh272c6132020-11-14 16:37:52 -0800592 DEPENDS flatc
593 COMMENT "Run generation: '${GEN_BINARY_SCHEMA}'")
594 register_generated_output(${GEN_BINARY_SCHEMA})
595endfunction()
596
597function(compile_flatbuffers_schema_to_embedded_binary SRC_FBS OPT)
598 if(FLATBUFFERS_BUILD_LEGACY)
599 set(OPT ${OPT};--cpp-std c++0x)
600 else()
601 # --cpp-std is defined by flatc default settings.
602 endif()
603 message(STATUS "`${SRC_FBS}`: add generation of C++ embedded binary schema code with '${OPT}'")
604 get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH)
605 string(REGEX REPLACE "\\.fbs$" "_bfbs_generated.h" GEN_BFBS_HEADER ${SRC_FBS})
James Kuszmaul8e62b022022-03-22 09:33:25 -0700606 # For details about flags see generate_code.py
Austin Schuh272c6132020-11-14 16:37:52 -0800607 add_custom_command(
608 OUTPUT ${GEN_BFBS_HEADER}
609 COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}"
610 --cpp --gen-mutable --gen-object-api --reflect-names
611 --cpp-ptr-type flatbuffers::unique_ptr # Used to test with C++98 STLs
612 ${OPT}
613 --bfbs-comments --bfbs-builtins --bfbs-gen-embed
James Kuszmaul8e62b022022-03-22 09:33:25 -0700614 --bfbs-filenames ${SRC_FBS_DIR}
Austin Schuh272c6132020-11-14 16:37:52 -0800615 -I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test"
616 -o "${SRC_FBS_DIR}"
617 "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}"
618 DEPENDS flatc
619 COMMENT "Run generation: '${GEN_BFBS_HEADER}'")
620 register_generated_output(${GEN_BFBS_HEADER})
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700621endfunction()
622
623if(FLATBUFFERS_BUILD_TESTS)
Austin Schuh272c6132020-11-14 16:37:52 -0800624 file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/tests" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
625 file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/samples" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
626
627 # TODO Add (monster_test.fbs monsterdata_test.json)->monsterdata_test.mon
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700628 compile_flatbuffers_schema_to_cpp(tests/monster_test.fbs)
Austin Schuh272c6132020-11-14 16:37:52 -0800629 compile_flatbuffers_schema_to_binary(tests/monster_test.fbs)
James Kuszmaul8e62b022022-03-22 09:33:25 -0700630 compile_flatbuffers_schema_to_cpp_opt(tests/namespace_test/namespace_test1.fbs "--no-includes;--gen-compare;--gen-name-strings")
631 compile_flatbuffers_schema_to_cpp_opt(tests/namespace_test/namespace_test2.fbs "--no-includes;--gen-compare;--gen-name-strings")
632 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 -0800633 compile_flatbuffers_schema_to_cpp(tests/optional_scalars.fbs)
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700634 compile_flatbuffers_schema_to_cpp_opt(tests/native_type_test.fbs "")
Austin Schuh272c6132020-11-14 16:37:52 -0800635 compile_flatbuffers_schema_to_cpp_opt(tests/arrays_test.fbs "--scoped-enums;--gen-compare")
636 compile_flatbuffers_schema_to_binary(tests/arrays_test.fbs)
637 compile_flatbuffers_schema_to_embedded_binary(tests/monster_test.fbs "--no-includes;--gen-compare")
Austin Schuh2dd86a92022-09-14 21:19:23 -0700638 compile_flatbuffers_schema_to_cpp(tests/native_inline_table_test.fbs "--gen-compare")
James Kuszmaul3b15b0c2022-11-08 14:03:16 -0800639 compile_flatbuffers_schema_to_cpp(tests/alignment_test.fbs "--gen-compare")
Austin Schuh272c6132020-11-14 16:37:52 -0800640 if(NOT (MSVC AND (MSVC_VERSION LESS 1900)))
641 compile_flatbuffers_schema_to_cpp(tests/monster_extra.fbs) # Test floating-point NAN/INF.
642 endif()
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700643 include_directories(${CMAKE_CURRENT_BINARY_DIR}/tests)
644 add_executable(flattests ${FlatBuffers_Tests_SRCS})
Austin Schuh2dd86a92022-09-14 21:19:23 -0700645 target_link_libraries(flattests PRIVATE $<BUILD_INTERFACE:ProjectConfig>)
646
Austin Schuh272c6132020-11-14 16:37:52 -0800647 add_dependencies(flattests generated_code)
Austin Schuh2dd86a92022-09-14 21:19:23 -0700648
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700649 if(FLATBUFFERS_CODE_SANITIZE)
Austin Schuh272c6132020-11-14 16:37:52 -0800650 add_fsanitize_to_target(flattests ${FLATBUFFERS_CODE_SANITIZE})
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700651 endif()
652
653 compile_flatbuffers_schema_to_cpp(samples/monster.fbs)
Austin Schuh272c6132020-11-14 16:37:52 -0800654 compile_flatbuffers_schema_to_binary(samples/monster.fbs)
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700655 include_directories(${CMAKE_CURRENT_BINARY_DIR}/samples)
Austin Schuh2dd86a92022-09-14 21:19:23 -0700656
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700657 add_executable(flatsamplebinary ${FlatBuffers_Sample_Binary_SRCS})
Austin Schuh2dd86a92022-09-14 21:19:23 -0700658 target_link_libraries(flatsamplebinary PRIVATE $<BUILD_INTERFACE:ProjectConfig>)
Austin Schuh272c6132020-11-14 16:37:52 -0800659 add_dependencies(flatsamplebinary generated_code)
Austin Schuh2dd86a92022-09-14 21:19:23 -0700660
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700661 add_executable(flatsampletext ${FlatBuffers_Sample_Text_SRCS})
Austin Schuh2dd86a92022-09-14 21:19:23 -0700662 target_link_libraries(flatsampletext PRIVATE $<BUILD_INTERFACE:ProjectConfig>)
Austin Schuh272c6132020-11-14 16:37:52 -0800663 add_dependencies(flatsampletext generated_code)
Austin Schuh2dd86a92022-09-14 21:19:23 -0700664
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700665 add_executable(flatsamplebfbs ${FlatBuffers_Sample_BFBS_SRCS})
Austin Schuh2dd86a92022-09-14 21:19:23 -0700666 target_link_libraries(flatsamplebfbs PRIVATE $<BUILD_INTERFACE:ProjectConfig>)
Austin Schuh272c6132020-11-14 16:37:52 -0800667 add_dependencies(flatsamplebfbs generated_code)
668
669 if(FLATBUFFERS_BUILD_CPP17)
670 # Don't generate header for flattests_cpp17 target.
671 # This target uses "generated_cpp17/monster_test_generated.h"
Austin Schuh272c6132020-11-14 16:37:52 -0800672 add_executable(flattests_cpp17 ${FlatBuffers_Tests_CPP17_SRCS})
673 add_dependencies(flattests_cpp17 generated_code)
Austin Schuh2dd86a92022-09-14 21:19:23 -0700674 target_link_libraries(flattests_cpp17 PRIVATE $<BUILD_INTERFACE:ProjectConfig>)
Austin Schuh272c6132020-11-14 16:37:52 -0800675 target_compile_features(flattests_cpp17 PRIVATE cxx_std_17)
Austin Schuh2dd86a92022-09-14 21:19:23 -0700676
Austin Schuh272c6132020-11-14 16:37:52 -0800677 if(FLATBUFFERS_CODE_SANITIZE)
678 add_fsanitize_to_target(flattests_cpp17 ${FLATBUFFERS_CODE_SANITIZE})
679 endif()
680 endif(FLATBUFFERS_BUILD_CPP17)
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700681endif()
682
683if(FLATBUFFERS_BUILD_GRPCTEST)
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700684 if(NOT GRPC_INSTALL_PATH)
685 message(SEND_ERROR "GRPC_INSTALL_PATH variable is not defined. See grpc/README.md")
686 endif()
687 if(NOT PROTOBUF_DOWNLOAD_PATH)
688 message(SEND_ERROR "PROTOBUF_DOWNLOAD_PATH variable is not defined. See grpc/README.md")
689 endif()
690 INCLUDE_DIRECTORIES(${GRPC_INSTALL_PATH}/include)
691 INCLUDE_DIRECTORIES(${PROTOBUF_DOWNLOAD_PATH}/src)
James Kuszmaul8e62b022022-03-22 09:33:25 -0700692 find_package(Threads REQUIRED)
693 list(APPEND CMAKE_PREFIX_PATH ${GRPC_INSTALL_PATH})
694 find_package(absl CONFIG REQUIRED)
695 find_package(protobuf CONFIG REQUIRED)
696 find_package(gRPC CONFIG REQUIRED)
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700697 add_executable(grpctest ${FlatBuffers_GRPCTest_SRCS})
Austin Schuh272c6132020-11-14 16:37:52 -0800698 add_dependencies(grpctest generated_code)
Austin Schuh2dd86a92022-09-14 21:19:23 -0700699 target_link_libraries(grpctext
700 PRIVATE
701 $<BUILD_INTERFACE:ProjectConfig>
702 gRPC::grpc++_unsecure
703 gRPC::gpr
704 pthread
705 dl
706 )
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700707endif()
708
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700709if(FLATBUFFERS_INSTALL)
710 include(GNUInstallDirs)
711
712 install(DIRECTORY include/flatbuffers DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
713
714 set(FB_CMAKE_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/flatbuffers")
715
Austin Schuh2dd86a92022-09-14 21:19:23 -0700716 configure_file(CMake/flatbuffers-config-version.cmake.in flatbuffers-config-version.cmake @ONLY)
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700717 install(
Austin Schuh2dd86a92022-09-14 21:19:23 -0700718 FILES
719 "CMake/flatbuffers-config.cmake"
720 "CMake/BuildFlatBuffers.cmake"
721 "${CMAKE_CURRENT_BINARY_DIR}/flatbuffers-config-version.cmake"
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700722 DESTINATION ${FB_CMAKE_DIR}
723 )
724
725 if(FLATBUFFERS_BUILD_FLATLIB)
James Kuszmaul8e62b022022-03-22 09:33:25 -0700726 install(
Austin Schuh2dd86a92022-09-14 21:19:23 -0700727 TARGETS flatbuffers EXPORT FlatBuffersTargets
James Kuszmaul8e62b022022-03-22 09:33:25 -0700728 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
729 INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
730 )
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700731
Austin Schuh2dd86a92022-09-14 21:19:23 -0700732 install(EXPORT FlatBuffersTargets
733 FILE FlatBuffersTargets.cmake
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700734 NAMESPACE flatbuffers::
735 DESTINATION ${FB_CMAKE_DIR}
736 )
737 endif()
738
739 if(FLATBUFFERS_BUILD_FLATC)
740 install(
741 TARGETS flatc EXPORT FlatcTargets
742 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
743 )
744
745 install(
746 EXPORT FlatcTargets
747 FILE FlatcTargets.cmake
748 NAMESPACE flatbuffers::
749 DESTINATION ${FB_CMAKE_DIR}
750 )
751 endif()
752
753 if(FLATBUFFERS_BUILD_SHAREDLIB)
James Kuszmaul8e62b022022-03-22 09:33:25 -0700754 install(
Austin Schuh2dd86a92022-09-14 21:19:23 -0700755 TARGETS flatbuffers_shared EXPORT FlatBuffersSharedTargets
James Kuszmaul8e62b022022-03-22 09:33:25 -0700756 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
757 RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR}
758 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
759 INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
760 )
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700761
James Kuszmaul8e62b022022-03-22 09:33:25 -0700762 install(
Austin Schuh2dd86a92022-09-14 21:19:23 -0700763 EXPORT FlatBuffersSharedTargets
764 FILE FlatBuffersSharedTargets.cmake
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700765 NAMESPACE flatbuffers::
766 DESTINATION ${FB_CMAKE_DIR}
767 )
768 endif()
James Kuszmaul8e62b022022-03-22 09:33:25 -0700769
770 if(FLATBUFFERS_BUILD_SHAREDLIB OR FLATBUFFERS_BUILD_FLATLIB)
771 configure_file(CMake/flatbuffers.pc.in flatbuffers.pc @ONLY)
772 install(
773 FILES "${CMAKE_CURRENT_BINARY_DIR}/flatbuffers.pc"
774 DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
775 )
776 endif()
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700777endif()
778
779if(FLATBUFFERS_BUILD_TESTS)
780 enable_testing()
781
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700782 add_test(NAME flattests COMMAND flattests)
Austin Schuh272c6132020-11-14 16:37:52 -0800783 if(FLATBUFFERS_BUILD_CPP17)
784 add_test(NAME flattests_cpp17 COMMAND flattests_cpp17)
785 endif()
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700786 if(FLATBUFFERS_BUILD_GRPCTEST)
787 add_test(NAME grpctest COMMAND grpctest)
788 endif()
789endif()
790
Austin Schuh272c6132020-11-14 16:37:52 -0800791# This target is sync-barrier.
792# Other generate-dependent targets can depend on 'generated_code' only.
793get_generated_output(fbs_generated)
794if(fbs_generated)
795 # message(STATUS "Add generated_code target with files:${fbs_generated}")
796 add_custom_target(generated_code
797 DEPENDS ${fbs_generated}
798 COMMENT "All generated files were updated.")
799endif()
800
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700801include(CMake/BuildFlatBuffers.cmake)
802
803if(UNIX)
804 # Use of CPack only supported on Linux systems.
805 if(FLATBUFFERS_PACKAGE_DEBIAN)
806 include(CMake/PackageDebian.cmake)
Austin Schuh272c6132020-11-14 16:37:52 -0800807 include(CPack)
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700808 endif()
809 if (FLATBUFFERS_PACKAGE_REDHAT)
810 include(CMake/PackageRedhat.cmake)
Austin Schuh272c6132020-11-14 16:37:52 -0800811 include(CPack)
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700812 endif()
Austin Schuhe89fa2d2019-08-14 20:24:23 -0700813endif()
James Kuszmaul8e62b022022-03-22 09:33:25 -0700814
815# Include for running Google Benchmarks.
816if(FLATBUFFERS_BUILD_BENCHMARKS)
817 add_subdirectory(benchmarks)
818endif()
819
820# Add FlatBuffers::FlatBuffers interface, needed for FetchContent_Declare
821add_library(FlatBuffers INTERFACE)
822add_library(FlatBuffers::FlatBuffers ALIAS FlatBuffers)
823target_include_directories(
824 FlatBuffers
825 INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
Austin Schuh2dd86a92022-09-14 21:19:23 -0700826 $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/include>)