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