-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
74 lines (62 loc) · 2.41 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
cmake_minimum_required(VERSION 3.9)
project(FlatBuffersTests CXX)
enable_testing()
if(NOT GOOGLETEST_SRC_DIR)
set(GOOGLETEST_SRC_DIR "${CMAKE_SOURCE_DIR}/../googletest")
endif()
if(NOT FLATBUFFERS_SRC_DIR)
set(FLATBUFFERS_SRC_DIR "${CMAKE_SOURCE_DIR}/../flatbuffers")
endif()
# Prevent overriding the parent project's compiler/linker settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Request googlemock (use Matchers from gmock)
# googletest is subpart of googlemock
set(BUILD_GMOCK ON)
# Add googletest directly to our build. This defines the gtest and gtest_main targets.
add_subdirectory(${GOOGLETEST_SRC_DIR}
${CMAKE_CURRENT_BINARY_DIR}/googletest-build
EXCLUDE_FROM_ALL)
# Add flatbuffers
set(FLATBUFFERS_INSTALL OFF CACHE BOOL "")
set(FLATBUFFERS_BUILD_FLATHASH OFF CACHE BOOL "")
set(FLATBUFFERS_BUILD_TESTS ON CACHE BOOL "")
set(FLATBUFFERS_BUILD_PATH "${CMAKE_CURRENT_BINARY_DIR}/flatbuffers-build")
add_subdirectory(${FLATBUFFERS_SRC_DIR} ${FLATBUFFERS_BUILD_PATH})
function(compile_flatbuffers_schema_to_cpp SRC_FBS)
get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH)
string(REGEX REPLACE "\\.fbs$" "_generated.h" GEN_HEADER ${SRC_FBS})
add_custom_command(
OUTPUT "${GEN_HEADER}"
COMMAND $<TARGET_FILE:flatc>
--cpp
--no-includes
--scoped-enums
--gen-mutable
--gen-object-api
--reflect-names
-o "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS_DIR}"
"${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}"
DEPENDS flatc)
endfunction()
# Update flatbuffers_tests schema
compile_flatbuffers_schema_to_cpp(tests/test.fbs)
# Add executable
add_executable(flatbuffers_tests
tests/json_parser_1.cpp
# add generated headers to dependency list for auto update
tests/test_generated.h
)
# Use global define for reference to fbs files instead of copy to binary dir.
target_compile_definitions(flatbuffers_tests
PRIVATE
JSON_SAMPLES_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}/json_datasets/\"
)
target_compile_definitions(flatbuffers_tests
PRIVATE
FLATBUFFERS_FBS_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}/tests/\"
)
target_link_libraries(flatbuffers_tests PRIVATE gtest gtest_main gmock)
target_link_libraries(flatbuffers_tests PRIVATE flatbuffers)
add_dependencies(flatbuffers_tests flatc)
add_dependencies(flatbuffers_tests flattests)
add_test(NAME FlatbuffersJsonParserTest COMMAND flatbuffers_tests)