forked from keengo99/kasync
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CMakeLists.txt
185 lines (166 loc) · 7.24 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
cmake_minimum_required(VERSION 2.8.12)
# set the project name
project(kasync)
set(KASYNC_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
file(GLOB KASYNC_SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/src/*.c)
file(GLOB KASYNC_INCLUDE_FILES ${KASYNC_INCLUDE_DIR}/*.h)
message(STATUS "system is ${CMAKE_SYSTEM_NAME}")
#message(STATUS "current source dir is ${CMAKE_CURRENT_SOURCE_DIR}")
#message(STATUS "current binary dir is ${CMAKE_CURRENT_BINARY_DIR}")
option(KASYNC_TEST "build for test" OFF)
option(KSOCKET_SSL "enable tls/ssl support" ON)
option(KSOCKET_SSL_BIO "enable tls/ssl bio support")
option(OPENSSL_DIR "set openssl dir")
include(CheckIncludeFile)
include(CheckSymbolExists)
if (KSOCKET_SSL)
if (NOT OPENSSL_DIR)
include(FindOpenSSL)
if (NOT OPENSSL_FOUND)
message(STATUS "cann't find openssl")
set(KSOCKET_SSL OFF)
else()
message(STATUS "build with tls/ssl support ${OPENSSL_INCLUDE_DIR}")
list(APPEND KASYNC_INCLUDE_DIR ${OPENSSL_INCLUDE_DIR})
list(APPEND KASYNC_LIBS OpenSSL::SSL OpenSSL::Crypto)
set_target_properties(OpenSSL::SSL OpenSSL::Crypto PROPERTIES IMPORTED_GLOBAL True)
endif()
else()
list(APPEND KASYNC_INCLUDE_DIR ${OPENSSL_DIR}/include)
list(APPEND KASYNC_LIBS ssl crypto)
endif()
else()
unset(KSOCKET_SSL_BIO)
endif()
if (CMAKE_SYSTEM_NAME MATCHES "Windows")
list(APPEND KASYNC_LIBS ws2_32)
endif()
#message(STATUS "cmake_system_name is ${CMAKE_SYSTEM_NAME}")
if (CMAKE_SYSTEM_NAME MATCHES "Linux" OR CMAKE_SYSTEM_NAME MATCHES "Android")
list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
set(LINUX ON)
option(LINUX_IOURING "enable io_uring")
if (LINUX_IOURING)
message(STATUS "linux use io_uring")
list(APPEND KASYNC_LIBS uring)
endif()
if (CMAKE_SYSTEM_NAME MATCHES "Linux")
list(APPEND KASYNC_LIBS pthread)
endif()
elseif (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
set(FREEBSD ON)
list(APPEND KASYNC_LIBS pthread)
elseif (CMAKE_SYSTEM_NAME MATCHES "Darwin")
set(DARWIN ON)
set(ENABLE_LIBUCONTEXT ON)
set(LIBUCONTEXT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libucontext)
set(LIBUCONTEXT_LIBRARY ${CMAKE_CURRENT_SOURCE_DIR}/libucontext/libucontext.a)
add_custom_target(libucontext.a ALL
COMMAND ${CMAKE_MAKE_PROGRAM} FREESTANDING=yes libucontext.a
WORKING_DIRECTORY ${LIBUCONTEXT_DIR}
COMMENT "Original metis makefile target")
add_library(ucontext STATIC IMPORTED GLOBAL)
set_property(TARGET ucontext APPEND PROPERTY IMPORTED_CONFIGURATIONS NOCONFIG)
set_target_properties(ucontext PROPERTIES IMPORTED_LOCATION_NOCONFIG "${LIBUCONTEXT_LIBRARY}")
list(APPEND KASYNC_LIBS ucontext)
list(APPEND KASYNC_INCLUDE_DIR ${LIBUCONTEXT_DIR}/include)
endif()
check_symbol_exists(accept4 sys/socket.h HAVE_ACCEPT4)
check_symbol_exists(memalign malloc.h HAVE_MEMALIGN)
check_symbol_exists(setmntent mntent.h HAVE_SETMNTENT)
if (CMAKE_ANDROID_ARCH STREQUAL "arm")
enable_language(ASM)
set(SHARED ON)
list(APPEND KASYNC_SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/ucontext/arm/makecontext.c)
list(APPEND KASYNC_SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/ucontext/arm/getcontext.S)
list(APPEND KASYNC_SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/ucontext/arm/setcontext.S)
list(APPEND KASYNC_SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/ucontext/arm/swapcontext.S)
list(APPEND KASYNC_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/ucontext/arm)
elseif (CMAKE_ANDROID_ARCH STREQUAL "arm64")
enable_language(ASM)
set(SHARED ON)
list(APPEND KASYNC_SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/ucontext/aarch64/makecontext.c)
list(APPEND KASYNC_SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/ucontext/aarch64/sysdep.c)
list(APPEND KASYNC_SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/ucontext/aarch64/getcontext.S)
list(APPEND KASYNC_SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/ucontext/aarch64/setcontext.S)
list(APPEND KASYNC_SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/ucontext/aarch64/swapcontext.S)
list(APPEND KASYNC_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/ucontext/aarch64)
elseif (CMAKE_ANDROID_ARCH STREQUAL "x86" OR CMAKE_ANDROID_ARCH STREQUAL "x86_64" )
set(DISABLE_KFIBER ON)
set(SHARED ON)
endif()
if (GTEST_ROOT)
set(USE_CXX)
set(CMAKE_CXX_STANDARD 14)
#force use cxx
file(GLOB KASYNC_FIBER_TEST_SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/test/kfiter_test/*.cc)
set(KASYNC_TEST_INCLUDE_DIR ${KASYNC_INCLUDE_DIR})
list(APPEND KASYNC_FIBER_TEST_SOURCE_FILES ${KASYNC_SOURCE_FILES})
#set(KASYNC_TEST_LIBS kasync)
#set(KASYNC_TEST_LIBS_DIR ../../build)
if (KSOCKET_SSL)
include(FindOpenSSL)
if (NOT OPENSSL_FOUND)
message(FATAL_ERROR "cann't found ssl")
endif()
list(APPEND KASYNC_TEST_LIBS OpenSSL::SSL OpenSSL::Crypto)
list(APPEND KASYNC_TEST_INCLUDE_DIR ${OPENSSL_INCLUDE_DIR})
endif()
if (CMAKE_SYSTEM_NAME MATCHES "Windows")
#windows
list(APPEND KASYNC_TEST_LIBS ws2_32)
else()
add_compile_options(-g)
add_compile_options(-O0)
endif()
if (CMAKE_SYSTEM_NAME MATCHES "Linux")
#linux
add_compile_options(-Wno-invalid-offsetof)
if (LINUX_IOURING)
list(APPEND KASYNC_TEST_LIBS uring)
endif()
list(APPEND KASYNC_TEST_LIBS pthread)
elseif (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
list(APPEND KASYNC_TEST_LIBS pthread)
endif()
message(STATUS ${KASYNC_FIBER_TEST_SOURCE_FILES})
list(APPEND KASYNC_FIBER_TEST_SOURCE_FILES ${GTEST_ROOT}/src/gtest-all.cc)
list(APPEND KASYNC_TEST_INCLUDE_DIR ${GTEST_ROOT} ${GTEST_ROOT}/include)
list(APPEND KASYNC_TEST_INCLUDE_DIR ${GTEST_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR}/test/)
message(STATUS "gtest include ${GTEST_INCLUDE_DIRS}")
add_executable(kasync_fiber_test ${KASYNC_FIBER_TEST_SOURCE_FILES})
target_include_directories(kasync_fiber_test PUBLIC ${KASYNC_TEST_INCLUDE_DIR})
target_link_directories(kasync_fiber_test PUBLIC ${KASYNC_TEST_LIBS_DIR})
target_link_libraries(kasync_fiber_test ${KASYNC_TEST_LIBS} ${KASYNC_LIBS})
endif()
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/include/kasync_config.h.in ${CMAKE_CURRENT_SOURCE_DIR}/include/kasync_config.h)
if (USE_CXX)
message(STATUS "use c++ to build kasync libs")
set_source_files_properties(${KASYNC_SOURCE_FILES} PROPERTIES LANGUAGE CXX)
endif()
get_directory_property(hasParent PARENT_DIRECTORY)
message(STATUS ${KASYNC_INCLUDE_DIR})
if(hasParent)
set(KASYNC_INCLUDE_DIR ${KASYNC_INCLUDE_DIR} PARENT_SCOPE)
set(KASYNC_LIBS ${KASYNC_LIBS} PARENT_SCOPE)
set(KASYNC_SOURCE_FILES ${KASYNC_SOURCE_FILES} PARENT_SCOPE)
endif()
if (SHARED)
message(STATUS "build as shared libary")
set(WINDOWS_EXPORT_ALL_SYMBOLS ON)
#set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
add_library(kasync SHARED ${KASYNC_SOURCE_FILES} ${KASYNC_INCLUDE_FILES})
target_link_libraries(kasync ${KASYNC_LIBS})
else()
message(STATUS "build as static libary")
add_library(kasync STATIC ${KASYNC_SOURCE_FILES} ${KASYNC_INCLUDE_FILES})
endif()
if (USE_CXX)
set_target_properties(kasync PROPERTIES LINKER_LANGUAGE CXX)
endif()
target_include_directories(kasync PUBLIC ${KASYNC_INCLUDE_DIR})
add_executable(pingpang ${CMAKE_CURRENT_SOURCE_DIR}/example/pingpang.c)
target_link_libraries(pingpang kasync ${KASYNC_LIBS})
if (ENABLE_LIBUCONTEXT)
add_dependencies(kasync ucontext)
endif()