1# Setup for running Google Benchmarks (https://github.com/google/benchmark) on 2# flatbuffers. This requires both that benchmark library and its depenency gtest 3# to build. Instead of including them here or doing a submodule, this uses 4# FetchContent (https://cmake.org/cmake/help/latest/module/FetchContent.html) to 5# grab the dependencies at config time. This requires CMake 3.14 or higher. 6cmake_minimum_required(VERSION 3.14) 7include(FetchContent) 8 9# No particular reason for the specific GIT_TAGs for the following repos, they 10# were just the latest releases when this was added. 11FetchContent_Declare( 12 googletest 13 GIT_REPOSITORY https://github.com/google/googletest.git 14 GIT_TAG e2239ee6043f73722e7aa812a459f54a28552929 # release-1.11.0 15) 16FetchContent_Declare( 17 googlebenchmark 18 GIT_REPOSITORY https://github.com/google/benchmark.git 19 GIT_TAG 0d98dba29d66e93259db7daa53a9327df767a415 # v1.6.1 20) 21 22# For Windows: Prevent overriding the parent project's compiler/linker 23# settings. 24set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) 25FetchContent_MakeAvailable( 26 googletest 27 googlebenchmark 28) 29 30set(CPP_BENCH_DIR cpp) 31set(CPP_FB_BENCH_DIR ${CPP_BENCH_DIR}/flatbuffers) 32set(CPP_RAW_BENCH_DIR ${CPP_BENCH_DIR}/raw) 33set(CPP_BENCH_FBS ${CPP_FB_BENCH_DIR}/bench.fbs) 34set(CPP_BENCH_FB_GEN ${CPP_FB_BENCH_DIR}/bench_generated.h) 35 36set(FlatBenchmark_SRCS 37 ${CPP_BENCH_DIR}/benchmark_main.cpp 38 ${CPP_FB_BENCH_DIR}/fb_bench.cpp 39 ${CPP_RAW_BENCH_DIR}/raw_bench.cpp 40 ${CPP_BENCH_FB_GEN} 41) 42 43# Generate the flatbuffers benchmark code from the flatbuffers schema using 44# flatc itself, thus it depends on flatc. This also depends on the C++ runtime 45# flatbuffers and the schema file itself, so it should auto-generated at the 46# correct times. 47add_custom_command( 48 OUTPUT ${CPP_BENCH_FB_GEN} 49 COMMAND 50 "${FLATBUFFERS_FLATC_EXECUTABLE}" 51 --cpp 52 -o ${CPP_FB_BENCH_DIR} 53 ${CPP_BENCH_FBS} 54 DEPENDS 55 flatc 56 flatbuffers 57 ${CPP_BENCH_FBS} 58 COMMENT "Run Flatbuffers Benchmark Codegen: ${CPP_BENCH_FB_GEN}" 59 VERBATIM) 60 61# The main flatbuffers benchmark executable 62add_executable(flatbenchmark ${FlatBenchmark_SRCS}) 63 64# Benchmark requires C++11 65target_compile_features(flatbenchmark PUBLIC 66 cxx_std_11 67) 68 69target_compile_options(flatbenchmark 70 PRIVATE 71 -fno-aligned-new 72 -Wno-deprecated-declarations 73) 74 75# Set the output directory to the root binary directory 76set_target_properties(flatbenchmark 77 PROPERTIES RUNTIME_OUTPUT_DIRECTORY 78 "${CMAKE_BINARY_DIR}" 79) 80 81# The includes of the benchmark files are fully qualified from flatbuffers root. 82target_include_directories(flatbenchmark PUBLIC ${CMAKE_SOURCE_DIR}) 83 84target_link_libraries(flatbenchmark 85 benchmark::benchmark_main # _main to use their entry point 86 gtest # Link to gtest so we can also assert in the benchmarks 87)