forked from yyzybb537/libgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
305 lines (264 loc) · 11.2 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
292
293
294
295
296
297
298
299
300
301
302
303
304
cmake_minimum_required(VERSION 2.8)
###################################################################################
project(libgo)
if (CMAKE_BUILD_TYPE)
else()
set(CMAKE_BUILD_TYPE RELEASE)
endif()
message("------------ Options -------------")
message(" CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
aux_source_directory(libgo CO_SRC_LIST)
message (" BOOST_ROOT: ${BOOST_ROOT}")
option(DEPEND_BOOST "link boost" OFF)
if (ENABLE_BOOST_COROUTINE)
include (${PROJECT_SOURCE_DIR}/boost.cmake)
message (" layer_context: boost.coroutine")
set(DEPEND_BOOST ON)
set(USE_BOOST_COROUTINE 1)
set(USE_BOOST_CONTEXT 0)
set(USE_UCONTEXT 0)
set(USE_FIBER 0)
else()
if (ENABLE_BOOST_CONTEXT)
include (${PROJECT_SOURCE_DIR}/boost.cmake)
message (" layer_context: boost.context")
set(DEPEND_BOOST ON)
set(USE_BOOST_COROUTINE 0)
set(USE_BOOST_CONTEXT 1)
set(USE_UCONTEXT 0)
set(USE_FIBER 0)
else()
set(USE_BOOST_COROUTINE 0)
set(USE_BOOST_CONTEXT 0)
if (UNIX)
set(USE_UCONTEXT 1)
set(USE_FIBER 0)
message (" layer_context: ucontext")
else()
set(USE_UCONTEXT 0)
set(USE_FIBER 1)
message (" layer_context: fiber")
endif()
endif()
endif()
option(WITH_CARES "compile with libcares" OFF)
if (WITH_CARES)
set(WITH_CARES 1)
message (" use cares: yes")
else()
set(WITH_CARES 0)
message (" use cares: no")
endif()
option(WITH_SAFE_SIGNAL "compile with SAFE_SIGNAL" OFF)
if (WITH_SAFE_SIGNAL)
set(WITH_SAFE_SIGNAL 1)
message (" use safe signal: yes")
else()
set(WITH_SAFE_SIGNAL 0)
message (" use safe signal: no")
endif()
option(LIBGO_SINGLE_THREAD "single thread mode" OFF)
if (LIBGO_SINGLE_THREAD)
set(LIBGO_SINGLE_THREAD 1)
message (" single thread mode: yes")
else()
set(LIBGO_SINGLE_THREAD 0)
message (" single thread mode: no")
endif()
option(ENABLE_DEBUGGER "enable debugger" OFF)
if (ENABLE_DEBUGGER)
set(ENABLE_DEBUGGER 1)
message (" enable_debugger: yes")
else()
set(ENABLE_DEBUGGER 0)
message (" enable_debugger: no")
endif()
if (DISABLE_HOOK)
message (" enable_hook: no")
else()
message (" enable_hook: yes")
endif()
if (DISABLE_DYNAMIC_LIB)
message (" build_dynamic_lib: no")
else()
message (" build_dynamic_lib: yes")
endif()
configure_file(${PROJECT_SOURCE_DIR}/libgo/cmake_config.h.in ${PROJECT_SOURCE_DIR}/libgo/cmake_config.h)
message("----------------------------------")
message("-------------- Env ---------------")
message(" CMAKE_SOURCE_DIR: ${CMAKE_SOURCE_DIR}")
message(" CMAKE_BINARY_DIR: ${CMAKE_BINARY_DIR}")
message("----------------------------------")
# 选项互斥检查
if (UNIX)
# unix like platforms
aux_source_directory(${PROJECT_SOURCE_DIR}/libgo/linux CO_SRC_LIST)
include_directories(${PROJECT_SOURCE_DIR}/libgo/linux)
if (DEPEND_BOOST)
set(LINK_LIBS "-lboost_coroutine -lboost_context -lboost_thread -lboost_system -pthread")
else ()
aux_source_directory(${PROJECT_SOURCE_DIR}/libgo/ctx_ucontext CO_SRC_LIST)
endif()
if (DISABLE_HOOK)
list(REMOVE_ITEM CO_SRC_LIST ${PROJECT_SOURCE_DIR}/libgo/linux/linux_glibc_hook.cpp)
aux_source_directory(${PROJECT_SOURCE_DIR}/libgo/disable_hook CO_SRC_LIST)
endif()
elseif (WIN32)
# windows platform
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
include_directories(${PROJECT_SOURCE_DIR}/libgo/windows)
aux_source_directory(${PROJECT_SOURCE_DIR}/libgo/windows CO_SRC_LIST)
aux_source_directory(${PROJECT_SOURCE_DIR}/libgo/windows/arpa CO_SRC_LIST)
aux_source_directory(${PROJECT_SOURCE_DIR}/libgo/windows/sys CO_SRC_LIST)
if (DEPEND_BOOST)
message(WARNING "boost.coroutine was unstable in windows platform")
else()
aux_source_directory(${PROJECT_SOURCE_DIR}/libgo/ctx_win_fiber CO_SRC_LIST)
endif()
if (DISABLE_HOOK)
list(REMOVE_ITEM CO_SRC_LIST ${PROJECT_SOURCE_DIR}/libgo/windows/win_vc_hook.cpp)
aux_source_directory(${PROJECT_SOURCE_DIR}/libgo/disable_hook CO_SRC_LIST)
else ()
list(APPEND CO_SRC_LIST "${PROJECT_SOURCE_DIR}/third_party/xhook/src/xhook.cpp")
include_directories(${PROJECT_SOURCE_DIR}/third_party/xhook/src)
endif()
else ()
# other platforms
message(FATAL_ERROR "Not support current platform!")
endif()
if (UNIX)
set(CMAKE_CXX_FLAGS "-std=c++11 -fPIC -Wall -pthread")
set(CMAKE_CXX_FLAGS_DEBUG "-g -pg ${CMAKE_CXX_FLAGS} -Werror")
set(CMAKE_CXX_FLAGS_RELEASE "-g -O3 ${CMAKE_CXX_FLAGS} -Werror")
else ()
if (NOT DISABLE_DYNAMIC_LIB)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MDd")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MD")
else()
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
endif()
endif()
message("------------ Cxx flags -------------")
message(" CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}: ${CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE}}")
message("------------------------------------")
include_directories(${PROJECT_SOURCE_DIR})
set(TARGET "libgo")
set(STATIC_T "libgo_static")
add_library("${STATIC_T}" STATIC ${CO_SRC_LIST})
set_target_properties("${STATIC_T}" PROPERTIES OUTPUT_NAME "${TARGET}")
if (UNIX)
add_subdirectory(${PROJECT_SOURCE_DIR}/libgo/main)
if (NOT DISABLE_DYNAMIC_LIB)
set(SHARED_T "libgo_dynamic")
add_library("${SHARED_T}" SHARED ${CO_SRC_LIST})
set_target_properties("${SHARED_T}" PROPERTIES COMPILE_FLAGS "-DCO_DYNAMIC_LINK")
set_target_properties("${SHARED_T}" PROPERTIES OUTPUT_NAME "${TARGET}")
target_link_libraries("${SHARED_T}" ${LINK_LIBS} -ldl)
install(TARGETS ${SHARED_T} LIBRARY DESTINATION "lib" ARCHIVE DESTINATION "lib")
endif()
install(TARGETS ${STATIC_T} LIBRARY DESTINATION "lib" ARCHIVE DESTINATION "lib")
install(DIRECTORY ${PROJECT_SOURCE_DIR}/libgo/ DESTINATION "include/libgo"
FILES_MATCHING
PATTERN "linux" EXCLUDE
PATTERN "main" EXCLUDE
PATTERN "windows" EXCLUDE
PATTERN "ctx_boost_coroutine" EXCLUDE
PATTERN "ctx_ucontext" EXCLUDE
PATTERN "*.h")
install(DIRECTORY ${PROJECT_SOURCE_DIR}/libgo/linux/ DESTINATION "include/libgo" FILES_MATCHING PATTERN "*.h")
file(WRITE ${PROJECT_SOURCE_DIR}/tools/libgo.conf "${CMAKE_INSTALL_PREFIX}/lib")
install(FILES ${PROJECT_SOURCE_DIR}/tools/libgo.conf DESTINATION "/etc/ld.so.conf.d")
if (DEPEND_BOOST)
install(DIRECTORY ${PROJECT_SOURCE_DIR}/libgo/ctx_boost_coroutine/ DESTINATION "include/libgo/ctx_boost_coroutine" FILES_MATCHING PATTERN "*.h")
install(DIRECTORY ${PROJECT_SOURCE_DIR}/libgo/ctx_boost_context/ DESTINATION "include/libgo/ctx_boost_context" FILES_MATCHING PATTERN "*.h")
else()
install(DIRECTORY ${PROJECT_SOURCE_DIR}/libgo/ctx_ucontext/ DESTINATION "include/libgo/ctx_ucontext" FILES_MATCHING PATTERN "*.h")
endif()
add_custom_target(debug
COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=DEBUG ${CMAKE_SOURCE_DIR}
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target all
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMENT "Switch CMAKE_BUILD_TYPE to Debug"
)
add_custom_target(release
COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=RELEASE ${CMAKE_SOURCE_DIR}
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target all
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMENT "Switch CMAKE_BUILD_TYPE to Release"
)
set(PROFILE_FLAGS "-g -pg -O3 -std=c++11 -Wall -fPIC")
#set(PROFILE_FLAGS "-g -pg ${CMAKE_CXX_FLAGS_RELEASE}")
#message("PROFILE_FLAGS: ${PROFILE_FLAGS}")
add_custom_target(profile
COMMAND ${CMAKE_COMMAND} -DCMAKE_BUILD_TYPE=PROFILE -DCMAKE_CXX_FLAGS_PROFILE=\\'${PROFILE_FLAGS}\\' ${CMAKE_SOURCE_DIR}
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target all
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMENT "Switch CMAKE_BUILD_TYPE to PROFILE"
)
add_custom_target(uninstall
COMMAND rm ${CMAKE_INSTALL_PREFIX}/lib/liblibgo.a ${CMAKE_INSTALL_PREFIX}/lib/liblibgo.so ${CMAKE_INSTALL_PREFIX}/lib/liblibgo_main.a -f
COMMAND rm ${CMAKE_INSTALL_PREFIX}/include/libgo -rf
)
add_custom_target(test_all
COMMAND ${CMAKE_COMMAND} -DOPEN_SUBDIRECTORY_TEST_ALL=ON -DOPEN_SUBDIRECTORY_TEST_DYNAMIC=OFF -DSMALL_TEST=OFF -DTEST_STATIC_LINK=ON -DTEST_LINK_FLAGS="${TEST_LINK_FLAGS}" ${CMAKE_SOURCE_DIR}
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target all
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMENT "Add subdirectory test_all"
)
option(OPEN_SUBDIRECTORY_TEST_ALL "" OFF)
if (OPEN_SUBDIRECTORY_TEST_ALL)
add_subdirectory("${PROJECT_SOURCE_DIR}/test/gtest_unit")
endif()
add_custom_target(test_dynamic
COMMAND ${CMAKE_COMMAND} -DOPEN_SUBDIRECTORY_TEST_DYNAMIC=ON -DOPEN_SUBDIRECTORY_TEST_ALL=OFF -DSMALL_TEST=OFF -DTEST_STATIC_LINK=OFF -DTEST_LINK_FLAGS="${TEST_LINK_FLAGS}" ${CMAKE_SOURCE_DIR}
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target all
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMENT "Add subdirectory test_dynamic"
)
option(OPEN_SUBDIRECTORY_TEST_DYNAMIC "" OFF)
if (OPEN_SUBDIRECTORY_TEST_DYNAMIC)
add_subdirectory("${PROJECT_SOURCE_DIR}/test/gtest_unit")
endif()
add_custom_target(test_small
COMMAND ${CMAKE_COMMAND} -DOPEN_SUBDIRECTORY_TEST_DYNAMIC=OFF -DOPEN_SUBDIRECTORY_TEST_ALL=OFF -DSMALL_TEST=ON -DTEST_STATIC_LINK=OFF -DTEST_LINK_FLAGS="${TEST_LINK_FLAGS}" ${CMAKE_SOURCE_DIR}
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target all
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMENT "Add subdirectory test_small"
)
option(SMALL_TEST "" OFF)
if (SMALL_TEST)
add_subdirectory("${PROJECT_SOURCE_DIR}/test/gtest_unit")
endif()
add_custom_target(bm
COMMAND ${CMAKE_COMMAND} -DOPEN_SUBDIRECTORY_BENCHMARK=ON ${CMAKE_SOURCE_DIR}
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target all
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMENT "Add subdirectory benchmark"
)
option(OPEN_SUBDIRECTORY_BENCHMARK "" OFF)
if (OPEN_SUBDIRECTORY_BENCHMARK)
add_subdirectory("${PROJECT_SOURCE_DIR}/test/benchmark")
endif()
add_custom_target(tutorial
COMMAND ${CMAKE_COMMAND} -DOPEN_SUBDIRECTORY_TUTORIAL=ON ${CMAKE_SOURCE_DIR}
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target all
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMENT "Add subdirectory tutorial"
)
option(OPEN_SUBDIRECTORY_TUTORIAL "" OFF)
if (OPEN_SUBDIRECTORY_TUTORIAL)
add_subdirectory("${PROJECT_SOURCE_DIR}/tutorial")
endif()
add_custom_target(run_test
COMMAND cp ${PROJECT_SOURCE_DIR}/test/gtest_unit/run_test . && ./run_test
)
else ()
add_subdirectory(${PROJECT_SOURCE_DIR}/libgo/main)
set_target_properties("${STATIC_T}" PROPERTIES COMPILE_FLAGS "/wd4819 /wd4267")
if (BOOST_ROOT)
add_subdirectory("${PROJECT_SOURCE_DIR}/test/gtest_unit")
add_subdirectory("${PROJECT_SOURCE_DIR}/test/benchmark")
add_subdirectory(${PROJECT_SOURCE_DIR}/tutorial)
endif()
endif()