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