This repository has been archived by the owner on Jul 8, 2022. It is now read-only.
forked from google/flatbuffers
-
Notifications
You must be signed in to change notification settings - Fork 4
/
CMakeLists.txt
210 lines (176 loc) · 7.28 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
cmake_minimum_required(VERSION 2.8)
if(CMAKE_VERSION VERSION_LESS 3.0)
project(FlatBuffers)
set(FlatBuffers_VERSION 1.0.0)
else()
cmake_policy(SET CMP0048 NEW)
project(FlatBuffers VERSION 1.0.0)
endif()
# NOTE: Code coverage only works on Linux & OSX.
option(FLATBUFFERS_CODE_COVERAGE "Enable the code coverage build option." OFF)
option(FLATBUFFERS_BUILD_TESTS "Enable the build of tests and samples." ON)
option(FLATBUFFERS_INSTALL "Enable the installation of targets." ON)
option(FLATBUFFERS_USE_CXX03_STDLIB
"Use boost classes to enable C++03 stdlib compatibility" OFF)
if(FLATBUFFERS_USE_CXX03_STDLIB)
find_package(Boost 1.41.0 REQUIRED)
include_directories(SYSTEM ${Boost_INCLUDE_DIRS})
endif()
configure_file(include/flatbuffers/options.in.h
"${PROJECT_BINARY_DIR}/include/flatbuffers/options.h")
include_directories("${PROJECT_BINARY_DIR}/include")
set(FlatBuffers_Library_SRCS
"${PROJECT_BINARY_DIR}/include/flatbuffers/options.h"
include/flatbuffers/flatbuffers.h
include/flatbuffers/idl.h
include/flatbuffers/util.h
src/base64.cpp
src/idl_parser.cpp
src/idl_gen_cpp.cpp
src/idl_gen_general.cpp
src/idl_gen_go.cpp
src/idl_gen_text.cpp
src/idl_gen_fbs.cpp
)
set(FlatBuffers_Compiler_SRCS
src/flatc.cpp
)
set(FlatBuffers_Tests_SRCS
tests/test.cpp
# file generate by running compiler on tests/monster_test.fbs
${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h
)
set(FlatBuffers_Sample_Binary_SRCS
samples/sample_binary.cpp
# file generated by running compiler on samples/monster.fbs
${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h
)
set(FlatBuffers_Sample_Text_SRCS
samples/sample_text.cpp
# file generated by running compiler on samples/monster.fbs
${CMAKE_CURRENT_BINARY_DIR}/samples/monster_generated.h
)
# source_group(Compiler FILES ${FlatBuffers_Library_SRCS})
# source_group(Compiler FILES ${FlatBuffers_Compiler_SRCS})
# source_group(Tests FILES ${FlatBuffers_Tests_SRCS})
if(APPLE)
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -std=c++11 -Wall -pedantic -Werror -Wextra")
if(NOT FLATBUFFERS_USE_CXX03_STDLIB)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
endif()
elseif(CMAKE_COMPILER_IS_GNUCXX OR
CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR
CMAKE_CXX_COMPILER_ID STREQUAL "Intel")
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -std=c++0x -Wall -pedantic -Werror -Wextra -fno-strict-aliasing")
endif()
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Qunused-arguments")
endif()
if(CMAKE_SYSTEM_NAME MATCHES "Linux")
add_definitions(-fPIC)
endif()
if(FLATBUFFERS_CODE_COVERAGE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fprofile-arcs -ftest-coverage")
set(CMAKE_EXE_LINKER_FLAGS
"${CMAKE_EXE_LINKER_FLAGS} -fprofile-arcs -ftest-coverage")
endif()
include_directories(include)
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 flatc -c -o "${SRC_FBS_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}"
DEPENDS flatc)
endfunction()
add_library(flatbuffers STATIC ${FlatBuffers_Library_SRCS})
add_executable(flatc src/flatc.cpp ${FlatBuffers_Compiler_SRCS})
target_link_libraries(flatc flatbuffers)
if(FLATBUFFERS_BUILD_TESTS)
compile_flatbuffers_schema_to_cpp(tests/monster_test.fbs)
include_directories(${CMAKE_CURRENT_BINARY_DIR}/tests)
add_executable(flattests ${FlatBuffers_Tests_SRCS})
target_link_libraries(flattests flatbuffers)
compile_flatbuffers_schema_to_cpp(samples/monster.fbs)
include_directories(${CMAKE_CURRENT_BINARY_DIR}/samples)
add_executable(flatsamplebinary ${FlatBuffers_Sample_Binary_SRCS})
target_link_libraries(flatsamplebinary flatbuffers)
add_executable(flatsampletext ${FlatBuffers_Sample_Text_SRCS})
target_link_libraries(flatsampletext flatbuffers)
enable_testing()
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/tests" DESTINATION
"${CMAKE_CURRENT_BINARY_DIR}")
add_test(NAME flattests COMMAND flattests)
# 'tests' target needed by BBP Jenkins CI
if(NOT TARGET ${PROJECT_NAME}-tests)
add_custom_target(${PROJECT_NAME}-tests COMMAND ${CMAKE_CTEST_COMMAND} DEPENDS flattests)
set_target_properties(${PROJECT_NAME}-tests PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
endif()
if(NOT TARGET tests)
add_custom_target(tests)
set_target_properties(tests PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD ON)
add_dependencies(tests ${PROJECT_NAME}-tests)
endif()
endif()
if(NOT FLATBUFFERS_INSTALL)
return()
endif()
#--------------------------------------------------
# INSTALL : setup files for projects that find_package(FlatBuffers)
# FlatBuffersTargets.cmake (any targets we export that another project needs)
# FlatBuffersConfigVersion.cmake (version info)
# FlatBuffersConfig.cmake (all the other variables another project needs)
#
# Supports three methods of using FlatBuffers
# find_package(FlatBuffers) - pointing to the INSTALL location
# find_package(FlatBuffers) - pointing to the BUILD directory
# add_subdirectory(FlatBuffers) - where the project using
# FlatBuffers can simply set FlatBuffers_DIR to the build subdirectoy
# The main difference is that the build dir versions need the source tree
# include locations instead of the install tree.
#--------------------------------------------------
set(ConfigPackageLocation share/cmake/FlatBuffers)
install(DIRECTORY include/flatbuffers DESTINATION include)
install(FILES "${PROJECT_BINARY_DIR}/include/flatbuffers/options.h"
DESTINATION include/flatbuffers)
install(TARGETS flatc
EXPORT FlatBuffersTargets
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
RUNTIME DESTINATION bin
PUBLIC_HEADER DESTINATION include)
install(TARGETS flatbuffers
EXPORT FlatBuffersTargets
ARCHIVE DESTINATION lib COMPONENT dev
RUNTIME DESTINATION bin COMPONENT lib
LIBRARY DESTINATION lib COMPONENT lib)
install(EXPORT FlatBuffersTargets
FILE FlatBuffersTargets.cmake DESTINATION .)
install(EXPORT FlatBuffersTargets
FILE FlatBuffersTargets.cmake DESTINATION ${ConfigPackageLocation})
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/FlatBuffersConfigVersion.cmake"
VERSION ${FlatBuffers_VERSION} COMPATIBILITY AnyNewerVersion)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/FlatBuffersConfigVersion.cmake"
DESTINATION ${ConfigPackageLocation} COMPONENT dev)
# Setup include files for the BUILD dir and source location
set(FLATBUFFERS_INCLUDE_DIRS
"${PROJECT_BINARY_DIR}/include"
"${CMAKE_CURRENT_SOURCE_DIR}/include")
configure_package_config_file(
"${CMAKE_CURRENT_SOURCE_DIR}/CMake/FlatBuffersConfig.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/FlatBuffersConfig.cmake"
INSTALL_DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/FlatBuffersConfig.cmake"
PATH_VARS FLATBUFFERS_INCLUDE_DIRS)
# Setup include files for the INSTALL dir
set(FLATBUFFERS_INCLUDE_DIRS "${CMAKE_INSTALL_PREFIX}/include")
configure_package_config_file(
"${CMAKE_CURRENT_SOURCE_DIR}/CMake/FlatBuffersConfig.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/FlatBuffersConfig.cmake"
INSTALL_DESTINATION ${ConfigPackageLocation}
PATH_VARS FLATBUFFERS_INCLUDE_DIRS)
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/FlatBuffersConfig.cmake"
DESTINATION ${ConfigPackageLocation} COMPONENT dev)