-
Notifications
You must be signed in to change notification settings - Fork 9
/
CMakeLists.txt
291 lines (256 loc) · 9.16 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
cmake_minimum_required(VERSION 3.1.0)
project(rellaf)
option(DEBUG "Print debug logs" OFF)
option(WITH_DEBUG_SYMBOLS "With debug symbols" OFF)
option(WITH_JSON "enable rellaf model to json converter" ON)
option(WITH_MYSQL "enable simple mysql connection pool" ON)
option(WITH_BRPC_EXT "enable brpc invoker" ON)
option(WITH_TEST "enable test" ON)
option(WITH_DEMO "CURD web demo" ON)
message(STATUS "CXX compiler: ${CMAKE_CXX_COMPILER}, version: "
"${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
message(STATUS "C compiler: ${CMAKE_C_COMPILER}, version: "
"${CMAKE_C_COMPILER_ID} ${CMAKE_C_COMPILER_VERSION}")
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
# require at least gcc 4.8
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.8)
message(FATAL_ERROR "GCC is too old, please install a newer version supporting C++11")
endif ()
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
# require at least clang 3.3
if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.3)
message(FATAL_ERROR "Clang is too old, please install a newer version supporting C++11")
endif ()
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
else ()
message(WARNING "You are using an unsupported compiler! Compilation has only been tested with Clang and GCC.")
endif ()
include(ProcessorCount)
ProcessorCount(NUM_OF_PROCESSOR)
message(NUM_OF_PROCESSOR: ${NUM_OF_PROCESSOR})
set(THIRD_DEPS)
set(SRC)
if (WITH_DEMO)
set(WITH_JSON ON)
set(WITH_MYSQL ON)
set(WITH_BRPC_EXT ON)
endif ()
if (WITH_JSON)
find_path(JSONCPP_INCLUDE_DIR NAMES json/json.h)
find_library(JSONCPP_LIBRARIES NAMES json_libmt jsoncpp)
if ((NOT JSONCPP_INCLUDE_DIR) OR (NOT JSONCPP_LIBRARIES))
message(FATAL_ERROR "Fail to find jsoncpp")
endif ()
include_directories(
${JSONCPP_INCLUDE_DIR}
)
file(GLOB JSON_SRC
src/json/*.h
src/json/*.hpp
src/json/*.cpp
)
list(APPEND SRC "${JSON_SRC}")
list(APPEND THIRD_DEPS ${JSONCPP_LIBRARIES})
endif ()
if (WITH_MYSQL)
find_path(MYSQL_INCLUDE_DIR NAMES mysql.h)
find_library(MYSQL_LIBRARIES NAMES mysqlclient)
if ((NOT MYSQL_INCLUDE_DIR) OR (NOT MYSQL_LIBRARIES))
message(FATAL_ERROR "Fail to find mysqlclient")
endif ()
include_directories(
${MYSQL_INCLUDE_DIR}
)
file(GLOB MYSQL_SRC
src/mysql/*.h
src/mysql/*.hpp
src/mysql/*.cpp
)
list(APPEND SRC "${MYSQL_SRC}")
list(APPEND THIRD_DEPS ${MYSQL_LIBRARIES})
endif ()
if (WITH_BRPC_EXT)
find_package(OpenSSL REQUIRED)
message(STATUS "ssl:" ${OPENSSL_SSL_LIBRARY})
message(STATUS "crypto:" ${OPENSSL_CRYPTO_LIBRARY})
find_path(BRPC_INC brpc/server.h)
find_library(BRPC_LIB NAMES libbrpc.a)
find_library(GFLAGS_LIB NAMES gflags)
find_library(PROTOBUF_LIB NAMES protobuf)
find_library(PROTOC_LIB NAMES protoc)
find_library(TCMALLOC_LIB NAMES tcmalloc)
find_library(LEVELDB_LIB NAMES leveldb)
include_directories(
${BRPC_INC}
include/brpc
)
file(GLOB BRPC_SRC
include/brpc/*.h
include/brpc/*.hpp
src/brpc/*.cpp
)
list(APPEND SRC "${BRPC_SRC}")
list(APPEND THIRD_DEPS ${BRPC_LIB})
list(APPEND THIRD_DEPS ${GFLAGS_LIB})
list(APPEND THIRD_DEPS ${TCMALLOC_LIB})
list(APPEND THIRD_DEPS ${PROTOBUF_LIB})
list(APPEND THIRD_DEPS ${PROTOC_LIB})
list(APPEND THIRD_DEPS ${LEVELDB_LIB})
endif ()
if (WITH_TEST)
find_path(GTEST_INCLUDE_PATH NAMES gtest/gtest.h)
find_library(GTEST_LIB NAMES gtest)
if ((NOT GTEST_INCLUDE_PATH) OR (NOT GTEST_LIB))
message(FATAL_ERROR "Fail to find gtest")
endif ()
include_directories(${GTEST_INCLUDE_PATH})
list(APPEND THIRD_DEPS ${GTEST_LIB})
endif ()
if (WITH_DEBUG_SYMBOLS)
set(DEBUG_SYMBOL "-ggdb")
endif ()
if (NOT DEBUG)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DNDEBUG -O2")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DNDEBUG -O2")
endif ()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 ${DEBUG_SYMBOL} -pipe -m64 -Wall -W -fPIC -Wno-unused-parameter -fno-omit-frame-pointer")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 ${DEBUG_SYMBOL} -pipe -m64 -Wall -W -fPIC -Wno-unused-parameter -fno-omit-frame-pointer")
macro(use_cxx11)
if (CMAKE_VERSION VERSION_LESS "3.1.3")
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
endif ()
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
endif ()
else ()
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif ()
endmacro(use_cxx11)
use_cxx11()
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(DEP_LIB ${DEP_LIB} rt)
elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(DEP_LIB ${DEP_LIB}
"-framework CoreFoundation"
"-framework CoreGraphics"
"-framework CoreData"
"-framework CoreText"
"-framework Security"
"-framework Foundation"
"-Wl,-U,_MallocExtension_ReleaseFreeMemory"
"-Wl,-U,_ProfilerStart"
"-Wl,-U,_ProfilerStop")
include(CheckFunctionExists)
CHECK_FUNCTION_EXISTS(clock_gettime HAVE_CLOCK_GETTIME)
if (NOT HAVE_CLOCK_GETTIME)
set(DEFINE_CLOCK_GETTIME "-DNO_CLOCK_GETTIME_IN_MAC")
endif ()
endif ()
add_definitions(
${DEFINE_CLOCK_GETTIME}
-D_GNU_SOURCE
-D__STDC_FORMAT_MACROS
-D__STDC_LIMIT_MACROS
-D__STDC_CONSTANT_MACROS
-DBRPC_WITH_GLOG=1
)
file(GLOB COMMON_SRC
include/*.h
include/*.hpp
src/*.h
src/*.hpp
src/*.cpp
)
list(APPEND SRC "${COMMON_SRC}")
set(INC
include
src
test/proto
)
message("THIRD_DEPS : ${THIRD_DEPS}")
add_library(rellaf STATIC ${SRC})
include_directories(${INC})
link_libraries(rellaf
${THIRD_DEPS}
pthread
dl
ssl
crypto
)
##### demo
if (WITH_DEMO)
# protobuf
include(FindProtobuf)
message("protoc : ${PROTOBUF_PROTOC_EXECUTABLE}, proto include : ${PROTOBUF_INCLUDE_DIRS}")
file(GLOB DEMO_SRC
demo/*.h
demo/*.hpp
demo/*.cpp
)
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/demo)
list(APPEND DEMO_SRC "${CMAKE_CURRENT_BINARY_DIR}/demo/cweb_service.pb.cc")
execute_process(
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
--cpp_out=${CMAKE_CURRENT_BINARY_DIR}/demo
--proto_path=${PROTOBUF_INCLUDE_DIRS}
--proto_path=${CMAKE_SOURCE_DIR}/demo ${CMAKE_SOURCE_DIR}/demo/cweb_service.proto
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
ERROR_VARIABLE PROTO_ERROR
RESULT_VARIABLE PROTO_RESULT
)
if (${PROTO_RESULT} EQUAL 0)
else ()
message(FATAL_ERROR "Fail to generate cpp of cweb_service.pb.cc : ${PROTO_ERROR}")
endif ()
include_directories(
demo
${CMAKE_CURRENT_BINARY_DIR}/demo
)
add_executable(cweb_demo ${DEMO_SRC})
add_dependencies(cweb_demo rellaf)
target_link_libraries(cweb_demo PUBLIC rellaf ${THIRD_DEPS})
endif ()
if (WITH_TEST)
add_executable(test_model test/test_model.cpp)
add_dependencies(test_model rellaf)
target_link_libraries(test_model PUBLIC rellaf ${THIRD_DEPS})
add_executable(test_enum test/test_enum.cpp)
add_dependencies(test_enum rellaf)
target_link_libraries(test_enum PUBLIC rellaf ${THIRD_DEPS})
add_executable(test_mapper test/test_sql_builder.cpp)
add_dependencies(test_mapper rellaf)
target_link_libraries(test_mapper PUBLIC rellaf ${THIRD_DEPS})
if (WITH_BRPC_EXT)
# protobuf
include(FindProtobuf)
message("protoc : ${PROTOBUF_PROTOC_EXECUTABLE}, proto include : ${PROTOBUF_INCLUDE_DIRS}")
file(GLOB PROTO_FILES "${CMAKE_SOURCE_DIR}/test/proto/*.proto")
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/proto)
foreach (PROTO ${PROTO_FILES})
message(proto : ${PROTO})
get_filename_component(PROTO_WE ${PROTO} NAME_WE)
list(APPEND PROTO_SRCS "${CMAKE_CURRENT_BINARY_DIR}/proto/${PROTO_WE}.pb.cc")
execute_process(
COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
--cpp_out=${CMAKE_CURRENT_BINARY_DIR}/proto
--proto_path=${PROTOBUF_INCLUDE_DIRS}
--proto_path=${CMAKE_SOURCE_DIR}/test/proto ${PROTO}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
ERROR_VARIABLE PROTO_ERROR
RESULT_VARIABLE PROTO_RESULT
)
if (${PROTO_RESULT} EQUAL 0)
else ()
message(FATAL_ERROR "Fail to generate cpp of ${PROTO} : ${PROTO_ERROR}")
endif ()
endforeach ()
message("protoc : ${PROTOBUF_PROTOC_EXECUTABLE}, proto srcs : ${PROTO_SRCS}")
include_directories(${CMAKE_CURRENT_BINARY_DIR}/proto)
add_executable(test_brpc_serive test/test_brpc_service.cpp ${PROTO_SRCS})
endif ()
if (WITH_JSON)
add_executable(test_json test/test_json.cpp)
endif ()
endif ()