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) |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 77 | option(FLATBUFFERS_STRICT_MODE |
| 78 | "Build flatbuffers with all warnings as errors (-Werror or /WX)." |
| 79 | OFF) |
| 80 | |
| 81 | if(NOT DEFINED FLATBUFFERS_CPP_STD) |
| 82 | set(FLATBUFFERS_CPP_STD 11) |
| 83 | endif() |
| 84 | |
| 85 | set(MSVC_LIKE OFF) |
| 86 | if(MSVC OR CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "MSVC") |
| 87 | set(MSVC_LIKE ON) |
| 88 | endif() |
| 89 | |
| 90 | if(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}") |
| 96 | endif() |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 97 | |
| 98 | if(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) |
| 102 | endif() |
| 103 | |
| 104 | if(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}") |
| 108 | endif() |
| 109 | |
| 110 | # Auto-detect locale-narrow 'strtod_l' and 'strtoull_l' functions. |
| 111 | if(NOT DEFINED FLATBUFFERS_LOCALE_INDEPENDENT) |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 112 | include(CheckCXXSymbolExists) |
| 113 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 114 | set(FLATBUFFERS_LOCALE_INDEPENDENT 0) |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 115 | if(MSVC_LIKE) |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 116 | 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() |
| 125 | endif() |
| 126 | add_definitions(-DFLATBUFFERS_LOCALE_INDEPENDENT=$<BOOL:${FLATBUFFERS_LOCALE_INDEPENDENT}>) |
| 127 | |
James Kuszmaul | 3b15b0c | 2022-11-08 14:03:16 -0800 | [diff] [blame] | 128 | if(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() |
| 133 | endif() |
| 134 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 135 | set(FlatBuffers_Library_SRCS |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 136 | include/flatbuffers/allocator.h |
| 137 | include/flatbuffers/array.h |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 138 | include/flatbuffers/base.h |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 139 | 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 Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 145 | include/flatbuffers/flatbuffers.h |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 146 | include/flatbuffers/flexbuffers.h |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 147 | include/flatbuffers/flex_flat_util.h |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 148 | include/flatbuffers/hash.h |
| 149 | include/flatbuffers/idl.h |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 150 | include/flatbuffers/minireflect.h |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 151 | include/flatbuffers/reflection.h |
| 152 | include/flatbuffers/reflection_generated.h |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 153 | include/flatbuffers/registry.h |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 154 | 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 Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 162 | src/idl_parser.cpp |
| 163 | src/idl_gen_text.cpp |
| 164 | src/reflection.cpp |
| 165 | src/util.cpp |
| 166 | ) |
| 167 | |
| 168 | set(FlatBuffers_Compiler_SRCS |
| 169 | ${FlatBuffers_Library_SRCS} |
| 170 | src/idl_gen_cpp.cpp |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 171 | src/idl_gen_csharp.cpp |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 172 | src/idl_gen_dart.cpp |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 173 | src/idl_gen_kotlin.cpp |
| 174 | src/idl_gen_go.cpp |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 175 | src/idl_gen_java.cpp |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 176 | src/idl_gen_ts.cpp |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 177 | 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 Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 185 | src/idl_gen_swift.cpp |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 186 | src/idl_namer.h |
| 187 | src/namer.h |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 188 | src/flatc.cpp |
| 189 | src/flatc_main.cpp |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 190 | src/bfbs_gen.h |
| 191 | src/bfbs_gen_lua.h |
James Kuszmaul | 3b15b0c | 2022-11-08 14:03:16 -0800 | [diff] [blame] | 192 | src/bfbs_gen_nim.h |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 193 | src/bfbs_namer.h |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 194 | include/flatbuffers/code_generators.h |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 195 | src/binary_annotator.h |
| 196 | src/binary_annotator.cpp |
| 197 | src/annotated_binary_text_gen.h |
| 198 | src/annotated_binary_text_gen.cpp |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 199 | src/bfbs_gen_lua.cpp |
James Kuszmaul | 3b15b0c | 2022-11-08 14:03:16 -0800 | [diff] [blame] | 200 | src/bfbs_gen_nim.cpp |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 201 | src/code_generators.cpp |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 202 | 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 Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 209 | grpc/src/compiler/python_generator.h |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 210 | 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 Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 215 | ) |
| 216 | |
| 217 | set(FlatHash_SRCS |
| 218 | include/flatbuffers/hash.h |
| 219 | src/flathash.cpp |
| 220 | ) |
| 221 | |
| 222 | set(FlatBuffers_Tests_SRCS |
| 223 | ${FlatBuffers_Library_SRCS} |
| 224 | src/idl_gen_fbs.cpp |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 225 | 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 Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 234 | tests/test.cpp |
| 235 | tests/test_assert.h |
| 236 | tests/test_assert.cpp |
| 237 | tests/test_builder.h |
| 238 | tests/test_builder.cpp |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 239 | tests/util_test.cpp |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 240 | tests/native_type_test_impl.h |
| 241 | tests/native_type_test_impl.cpp |
James Kuszmaul | 3b15b0c | 2022-11-08 14:03:16 -0800 | [diff] [blame] | 242 | tests/alignment_test.h |
| 243 | tests/alignment_test.cpp |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 244 | include/flatbuffers/code_generators.h |
| 245 | src/code_generators.cpp |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 246 | # file generate by running compiler on tests/monster_test.fbs |
| 247 | ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 248 | # 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 Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 253 | # 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 Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 257 | # 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 Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 263 | # 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 Kuszmaul | 3b15b0c | 2022-11-08 14:03:16 -0800 | [diff] [blame] | 265 | # file generate by running compiler on tests/alignment_test.fbs |
| 266 | ${CMAKE_CURRENT_BINARY_DIR}/tests/alignment_test_generated.h |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 267 | ) |
| 268 | |
| 269 | set(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 Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 279 | ) |
| 280 | |
| 281 | set(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 | |
| 288 | set(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 | |
| 295 | set(FlatBuffers_Sample_BFBS_SRCS |
| 296 | ${FlatBuffers_Library_SRCS} |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 297 | 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 | |
| 302 | set(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 Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 315 | # file generate by running compiler on tests/monster_test.fbs |
| 316 | ${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 317 | ) |
| 318 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 319 | # 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 Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 321 | # Append FLATBUFFERS_CXX_FLAGS to CMAKE_CXX_FLAGS. |
| 322 | if(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}") |
| 325 | endif() |
| 326 | message(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}") |
| 327 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 328 | function(add_fsanitize_to_target _target _sanitizer) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 329 | 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 Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 350 | endif() |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 351 | endif() |
| 352 | endfunction() |
| 353 | |
| 354 | function(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 Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 360 | endif() |
| 361 | endfunction() |
| 362 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 363 | include_directories(include) |
| 364 | include_directories(grpc) |
| 365 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 366 | # 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. |
| 371 | add_library(ProjectConfig INTERFACE) |
| 372 | target_compile_features(ProjectConfig |
| 373 | INTERFACE |
| 374 | cxx_std_${FLATBUFFERS_CPP_STD} |
| 375 | ) |
| 376 | |
| 377 | # Force the standard to be met. |
| 378 | set(CMAKE_CXX_STANDARD_REQUIRED ON) |
| 379 | |
| 380 | # We shouldn't rely on any compiler-extensions to make things work. |
| 381 | set(CMAKE_CXX_EXTENSIONS OFF) |
| 382 | |
| 383 | if(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 | ) |
| 397 | else() |
| 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() |
| 473 | endif() |
| 474 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 475 | if(FLATBUFFERS_BUILD_FLATLIB) |
| 476 | add_library(flatbuffers STATIC ${FlatBuffers_Library_SRCS}) |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 477 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 478 | # Attach header directory for when build via add_subdirectory(). |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 479 | 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 Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 485 | if(FLATBUFFERS_ENABLE_PCH) |
| 486 | add_pch_to_target(flatbuffers include/flatbuffers/pch/pch.h) |
| 487 | endif() |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 488 | endif() |
| 489 | |
| 490 | if(FLATBUFFERS_BUILD_FLATC) |
| 491 | add_executable(flatc ${FlatBuffers_Compiler_SRCS}) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 492 | if(FLATBUFFERS_ENABLE_PCH) |
| 493 | add_pch_to_target(flatc include/flatbuffers/pch/flatc_pch.h) |
| 494 | endif() |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 495 | |
| 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 Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 504 | 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 Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 510 | if(FLATBUFFERS_STATIC_FLATC AND NOT MSVC) |
| 511 | target_link_libraries(flatc PRIVATE -static) |
| 512 | endif() |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 513 | endif() |
| 514 | |
| 515 | if(FLATBUFFERS_BUILD_FLATHASH) |
| 516 | add_executable(flathash ${FlatHash_SRCS}) |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 517 | target_link_libraries(flathash PRIVATE $<BUILD_INTERFACE:ProjectConfig>) |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 518 | endif() |
| 519 | |
| 520 | if(FLATBUFFERS_BUILD_SHAREDLIB) |
| 521 | add_library(flatbuffers_shared SHARED ${FlatBuffers_Library_SRCS}) |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 522 | target_link_libraries(flatbuffers_shared PRIVATE $<BUILD_INTERFACE:ProjectConfig>) |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 523 | # 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 Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 527 | set(FlatBuffers_Library_SONAME_MAJOR ${VERSION_MAJOR}) |
| 528 | 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] | 529 | set_target_properties(flatbuffers_shared PROPERTIES OUTPUT_NAME flatbuffers |
| 530 | SOVERSION "${FlatBuffers_Library_SONAME_MAJOR}" |
| 531 | VERSION "${FlatBuffers_Library_SONAME_FULL}") |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 532 | if(FLATBUFFERS_ENABLE_PCH) |
| 533 | add_pch_to_target(flatbuffers_shared include/flatbuffers/pch/pch.h) |
| 534 | endif() |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 535 | endif() |
| 536 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 537 | # Global list of generated files. |
| 538 | # Use the global property to be independent of PARENT_SCOPE. |
| 539 | set_property(GLOBAL PROPERTY FBS_GENERATED_OUTPUTS) |
| 540 | |
| 541 | function(get_generated_output generated_files) |
| 542 | get_property(tmp GLOBAL PROPERTY FBS_GENERATED_OUTPUTS) |
| 543 | set(${generated_files} ${tmp} PARENT_SCOPE) |
| 544 | endfunction(get_generated_output) |
| 545 | |
| 546 | function(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}) |
| 550 | endfunction(register_generated_output) |
| 551 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 552 | function(compile_flatbuffers_schema_to_cpp_opt SRC_FBS OPT) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 553 | 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 Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 559 | 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 Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 563 | COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}" |
| 564 | --cpp --gen-mutable --gen-object-api --reflect-names |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 565 | --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] | 566 | ${OPT} |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 567 | -I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test" |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 568 | -o "${SRC_FBS_DIR}" |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 569 | "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}" |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 570 | DEPENDS flatc |
| 571 | COMMENT "Run generation: '${GEN_HEADER}'") |
| 572 | register_generated_output(${GEN_HEADER}) |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 573 | endfunction() |
| 574 | |
| 575 | function(compile_flatbuffers_schema_to_cpp SRC_FBS) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 576 | compile_flatbuffers_schema_to_cpp_opt(${SRC_FBS} "--no-includes;--gen-compare") |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 577 | endfunction() |
| 578 | |
| 579 | function(compile_flatbuffers_schema_to_binary SRC_FBS) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 580 | message(STATUS "`${SRC_FBS}`: add generation of binary (.bfbs) schema") |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 581 | get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH) |
| 582 | string(REGEX REPLACE "\\.fbs$" ".bfbs" GEN_BINARY_SCHEMA ${SRC_FBS}) |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 583 | # For details about flags see generate_code.py |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 584 | add_custom_command( |
| 585 | OUTPUT ${GEN_BINARY_SCHEMA} |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 586 | COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}" |
| 587 | -b --schema --bfbs-comments --bfbs-builtins |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 588 | --bfbs-filenames ${SRC_FBS_DIR} |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 589 | -I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test" |
| 590 | -o "${SRC_FBS_DIR}" |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 591 | "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}" |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 592 | DEPENDS flatc |
| 593 | COMMENT "Run generation: '${GEN_BINARY_SCHEMA}'") |
| 594 | register_generated_output(${GEN_BINARY_SCHEMA}) |
| 595 | endfunction() |
| 596 | |
| 597 | function(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 Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 606 | # For details about flags see generate_code.py |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 607 | 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 Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 614 | --bfbs-filenames ${SRC_FBS_DIR} |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 615 | -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 Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 621 | endfunction() |
| 622 | |
| 623 | if(FLATBUFFERS_BUILD_TESTS) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 624 | 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 Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 628 | compile_flatbuffers_schema_to_cpp(tests/monster_test.fbs) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 629 | compile_flatbuffers_schema_to_binary(tests/monster_test.fbs) |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 630 | 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 Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 633 | compile_flatbuffers_schema_to_cpp(tests/optional_scalars.fbs) |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 634 | compile_flatbuffers_schema_to_cpp_opt(tests/native_type_test.fbs "") |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 635 | 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 Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 638 | compile_flatbuffers_schema_to_cpp(tests/native_inline_table_test.fbs "--gen-compare") |
James Kuszmaul | 3b15b0c | 2022-11-08 14:03:16 -0800 | [diff] [blame] | 639 | compile_flatbuffers_schema_to_cpp(tests/alignment_test.fbs "--gen-compare") |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 640 | 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 Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 643 | include_directories(${CMAKE_CURRENT_BINARY_DIR}/tests) |
| 644 | add_executable(flattests ${FlatBuffers_Tests_SRCS}) |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 645 | target_link_libraries(flattests PRIVATE $<BUILD_INTERFACE:ProjectConfig>) |
| 646 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 647 | add_dependencies(flattests generated_code) |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 648 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 649 | if(FLATBUFFERS_CODE_SANITIZE) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 650 | add_fsanitize_to_target(flattests ${FLATBUFFERS_CODE_SANITIZE}) |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 651 | endif() |
| 652 | |
| 653 | compile_flatbuffers_schema_to_cpp(samples/monster.fbs) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 654 | compile_flatbuffers_schema_to_binary(samples/monster.fbs) |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 655 | include_directories(${CMAKE_CURRENT_BINARY_DIR}/samples) |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 656 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 657 | add_executable(flatsamplebinary ${FlatBuffers_Sample_Binary_SRCS}) |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 658 | target_link_libraries(flatsamplebinary PRIVATE $<BUILD_INTERFACE:ProjectConfig>) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 659 | add_dependencies(flatsamplebinary generated_code) |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 660 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 661 | add_executable(flatsampletext ${FlatBuffers_Sample_Text_SRCS}) |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 662 | target_link_libraries(flatsampletext PRIVATE $<BUILD_INTERFACE:ProjectConfig>) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 663 | add_dependencies(flatsampletext generated_code) |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 664 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 665 | add_executable(flatsamplebfbs ${FlatBuffers_Sample_BFBS_SRCS}) |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 666 | target_link_libraries(flatsamplebfbs PRIVATE $<BUILD_INTERFACE:ProjectConfig>) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 667 | 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 Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 672 | add_executable(flattests_cpp17 ${FlatBuffers_Tests_CPP17_SRCS}) |
| 673 | add_dependencies(flattests_cpp17 generated_code) |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 674 | target_link_libraries(flattests_cpp17 PRIVATE $<BUILD_INTERFACE:ProjectConfig>) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 675 | target_compile_features(flattests_cpp17 PRIVATE cxx_std_17) |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 676 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 677 | if(FLATBUFFERS_CODE_SANITIZE) |
| 678 | add_fsanitize_to_target(flattests_cpp17 ${FLATBUFFERS_CODE_SANITIZE}) |
| 679 | endif() |
| 680 | endif(FLATBUFFERS_BUILD_CPP17) |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 681 | endif() |
| 682 | |
| 683 | if(FLATBUFFERS_BUILD_GRPCTEST) |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 684 | 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 Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 692 | 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 Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 697 | add_executable(grpctest ${FlatBuffers_GRPCTest_SRCS}) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 698 | add_dependencies(grpctest generated_code) |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 699 | target_link_libraries(grpctext |
| 700 | PRIVATE |
| 701 | $<BUILD_INTERFACE:ProjectConfig> |
| 702 | gRPC::grpc++_unsecure |
| 703 | gRPC::gpr |
| 704 | pthread |
| 705 | dl |
| 706 | ) |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 707 | endif() |
| 708 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 709 | if(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 Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 716 | configure_file(CMake/flatbuffers-config-version.cmake.in flatbuffers-config-version.cmake @ONLY) |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 717 | install( |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 718 | FILES |
| 719 | "CMake/flatbuffers-config.cmake" |
| 720 | "CMake/BuildFlatBuffers.cmake" |
| 721 | "${CMAKE_CURRENT_BINARY_DIR}/flatbuffers-config-version.cmake" |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 722 | DESTINATION ${FB_CMAKE_DIR} |
| 723 | ) |
| 724 | |
| 725 | if(FLATBUFFERS_BUILD_FLATLIB) |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 726 | install( |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 727 | TARGETS flatbuffers EXPORT FlatBuffersTargets |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 728 | ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} |
| 729 | INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} |
| 730 | ) |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 731 | |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 732 | install(EXPORT FlatBuffersTargets |
| 733 | FILE FlatBuffersTargets.cmake |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 734 | 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 Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 754 | install( |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 755 | TARGETS flatbuffers_shared EXPORT FlatBuffersSharedTargets |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 756 | 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 Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 761 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 762 | install( |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 763 | EXPORT FlatBuffersSharedTargets |
| 764 | FILE FlatBuffersSharedTargets.cmake |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 765 | NAMESPACE flatbuffers:: |
| 766 | DESTINATION ${FB_CMAKE_DIR} |
| 767 | ) |
| 768 | endif() |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 769 | |
| 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 Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 777 | endif() |
| 778 | |
| 779 | if(FLATBUFFERS_BUILD_TESTS) |
| 780 | enable_testing() |
| 781 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 782 | add_test(NAME flattests COMMAND flattests) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 783 | if(FLATBUFFERS_BUILD_CPP17) |
| 784 | add_test(NAME flattests_cpp17 COMMAND flattests_cpp17) |
| 785 | endif() |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 786 | if(FLATBUFFERS_BUILD_GRPCTEST) |
| 787 | add_test(NAME grpctest COMMAND grpctest) |
| 788 | endif() |
| 789 | endif() |
| 790 | |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 791 | # This target is sync-barrier. |
| 792 | # Other generate-dependent targets can depend on 'generated_code' only. |
| 793 | get_generated_output(fbs_generated) |
| 794 | if(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.") |
| 799 | endif() |
| 800 | |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 801 | include(CMake/BuildFlatBuffers.cmake) |
| 802 | |
| 803 | if(UNIX) |
| 804 | # Use of CPack only supported on Linux systems. |
| 805 | if(FLATBUFFERS_PACKAGE_DEBIAN) |
| 806 | include(CMake/PackageDebian.cmake) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 807 | include(CPack) |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 808 | endif() |
| 809 | if (FLATBUFFERS_PACKAGE_REDHAT) |
| 810 | include(CMake/PackageRedhat.cmake) |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 811 | include(CPack) |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 812 | endif() |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 813 | endif() |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame] | 814 | |
| 815 | # Include for running Google Benchmarks. |
| 816 | if(FLATBUFFERS_BUILD_BENCHMARKS) |
| 817 | add_subdirectory(benchmarks) |
| 818 | endif() |
| 819 | |
| 820 | # Add FlatBuffers::FlatBuffers interface, needed for FetchContent_Declare |
| 821 | add_library(FlatBuffers INTERFACE) |
| 822 | add_library(FlatBuffers::FlatBuffers ALIAS FlatBuffers) |
| 823 | target_include_directories( |
| 824 | FlatBuffers |
| 825 | INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> |
Austin Schuh | 2dd86a9 | 2022-09-14 21:19:23 -0700 | [diff] [blame] | 826 | $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/include>) |