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