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