forked from luncliff/coroutine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
369 lines (339 loc) · 12.7 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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#
# github.com/luncliff (luncliff@gmail.com)
#
cmake_minimum_required(VERSION 3.12) # supports CXX_STANDARD 20
project(coroutine LANGUAGES CXX
VERSION 1.5.1
)
if(NOT DEFINED BUILD_SHARED_LIBS)
set(BUILD_SHARED_LIBS true)
endif()
if(NOT DEFINED CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
# set(CMAKE_CXX_STANDARD 20)
message(STATUS "system: ${CMAKE_SYSTEM}")
message(STATUS "build_type: ${CMAKE_BUILD_TYPE}")
message(STATUS "paths:")
message(STATUS " - ${PROJECT_SOURCE_DIR}")
message(STATUS " - ${CMAKE_INSTALL_PREFIX}")
message(STATUS " - ${CMAKE_SOURCE_DIR}")
message(STATUS " - ${CMAKE_CURRENT_SOURCE_DIR}")
message(STATUS)
message(STATUS "compiler:")
message(STATUS " - ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
message(STATUS " - ${CMAKE_CXX_COMPILER}")
message(STATUS)
message(STATUS "cmake:")
message(STATUS " - ${CMAKE_VERSION}")
message(STATUS " - ${CMAKE_COMMAND}")
message(STATUS " - ${CMAKE_TOOLCHAIN_FILE}")
message(STATUS " - ${CMAKE_GENERATOR}")
message(STATUS " - ${CMAKE_BUILD_TOOL}")
message(STATUS)
#
# import external libraries. see 'external/'
#
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
find_package(Microsoft.GSL CONFIG)
if(Microsoft.GSL_FOUND)
get_target_property(GSL_INCLUDE_DIR
Microsoft.GSL::GSL INTERFACE_INCLUDE_DIRECTORIES)
else()
add_subdirectory(external/ms-gsl)
get_filename_component(GSL_INCLUDE_DIR
${PROJECT_SOURCE_DIR}/external/ms-gsl/include ABSOLUTE)
endif()
message(STATUS "using ms-gsl: ${GSL_INCLUDE_DIR}")
message(STATUS)
#
# Acquire informations about current build environment. Especially for Compiler & STL
# - support_latest
# - support_coroutine
# - support_intrinsic_builtin: optional. compiler supports `__builtin_coro_*`
# - has_coroutine
# - has_coroutine_ts
#
include(CheckIncludeFileCXX)
include(CheckCXXCompilerFlag)
include(CheckCXXSourceCompiles)
# include(CheckCXXSymbolExists)
if(CMAKE_CXX_COMPILER_ID MATCHES Clang)
if(WIN32)
check_cxx_compiler_flag("/std:c++latest" support_latest)
check_cxx_compiler_flag("/clang:-fcoroutines-ts" support_coroutine)
check_include_file_cxx("experimental/coroutine" has_coroutine_ts
"/std:c++latest"
)
else()
check_cxx_compiler_flag("-std=c++2a" support_latest)
check_cxx_compiler_flag("-fcoroutines-ts" support_coroutine)
check_include_file_cxx("experimental/coroutine" has_coroutine_ts
"-std=c++2a"
)
endif()
elseif(MSVC)
#
# Notice that `/std:c++latest` and `/await` is exclusive to each other.
# With MSVC, we have to distinguish Coroutines TS & C++ 20 Coroutines
#
check_cxx_compiler_flag("/std:c++latest" support_latest)
check_cxx_compiler_flag("/await" support_coroutine)
check_include_file_cxx("coroutine" has_coroutine
"/std:c++latest"
)
if(NOT has_coroutine)
message(STATUS "Try <expeirmental/coroutine> (Coroutines TS) instead of <coroutine> ...")
check_include_file_cxx("experimental/coroutine" has_coroutine_ts
"/std:c++17"
)
endif()
# has coroutine headers?
if(NOT has_coroutine AND NOT has_coroutine_ts)
message(FATAL_ERROR "There are no headers for C++ Coroutines")
endif()
elseif(CMAKE_CXX_COMPILER_ID MATCHES GNU)
#
# expect GCC 10 or later
#
check_cxx_compiler_flag("-std=gnu++20" support_latest)
check_cxx_compiler_flag("-fcoroutines" support_coroutine)
check_include_file_cxx("coroutine" has_coroutine
"-std=gnu++20 -fcoroutines"
)
if(APPLE)
# -isysroot "/usr/local/Cellar/gcc/${CMAKE_CXX_COMPILER_VERSION}/include/c++/${CMAKE_CXX_COMPILER_VERSION}"
# -isysroot "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include"
# -isysroot "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1"
endif()
set(try_compile_flags "-fcoroutines")
endif()
# support compiler options for coroutine?
if(NOT support_coroutine)
message(FATAL_ERROR "The compiler doesn't support C++ Coroutines")
endif()
# support `__builtin_coro_*`?
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
get_filename_component(intrinsic_test_cpp
test/support_intrinsic_builtin.cpp ABSOLUTE)
try_compile(support_intrinsic_builtin ${CMAKE_BINARY_DIR}/supports
SOURCES ${intrinsic_test_cpp}
# This is a workaround. for some reasone CMAKE_FLAGS has no effect.
# Instead, place the compiler option where macro definition are placed
COMPILE_DEFINITIONS ${try_compile_flags}
CXX_STANDARD 20
OUTPUT_VARIABLE report
)
if(NOT support_intrinsic_builtin)
message(STATUS ${report})
endif()
message(STATUS "supports __builtin_coro: ${support_intrinsic_builtin}")
#
# Known STL-Compiler issues
# - https://github.com/microsoft/STL, issue 100
#
if(CMAKE_CXX_COMPILER_ID MATCHES Clang AND WIN32)
message(WARNING "clang-cl won't work with <experimental/coroutine>")
endif()
set(MODULE_INTERFACE_DIR ${PROJECT_SOURCE_DIR}/interface)
add_subdirectory(modules/portable) # corotuien_portable
add_subdirectory(modules/system) # coroutine_system
add_subdirectory(modules/net) # coroutine_net
install(FILES ${MODULE_INTERFACE_DIR}/coroutine/frame.h
${MODULE_INTERFACE_DIR}/coroutine/return.h
${MODULE_INTERFACE_DIR}/coroutine/channel.hpp
${MODULE_INTERFACE_DIR}/coroutine/yield.hpp
DESTINATION ${CMAKE_INSTALL_PREFIX}/include/coroutine
)
if(WIN32)
install(FILES ${MODULE_INTERFACE_DIR}/coroutine/windows.h
DESTINATION ${CMAKE_INSTALL_PREFIX}/include/coroutine
)
elseif(CMAKE_SYSTEM_NAME MATCHES Linux)
install(FILES ${MODULE_INTERFACE_DIR}/coroutine/linux.h
${MODULE_INTERFACE_DIR}/coroutine/pthread.h
DESTINATION ${CMAKE_INSTALL_PREFIX}/include/coroutine
)
elseif(UNIX OR APPLE)
install(FILES ${MODULE_INTERFACE_DIR}/coroutine/unix.h
${MODULE_INTERFACE_DIR}/coroutine/pthread.h
DESTINATION ${CMAKE_INSTALL_PREFIX}/include/coroutine
)
endif()
install(FILES ${MODULE_INTERFACE_DIR}/coroutine/net.h
DESTINATION ${CMAKE_INSTALL_PREFIX}/include/coroutine
)
install(TARGETS coroutine_portable coroutine_system coroutine_net
EXPORT coroutine-config
INCLUDES DESTINATION ${CMAKE_INSTALL_PREFIX}/include
RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin
LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
ARCHIVE DESTINATION ${CMAKE_INSTALL_PREFIX}/lib
)
#
# export declared cmake targets
#
# 'coroutine-targets' is indeed better name, but without using 'configure_file()'
# the exporting step will be more complicated for non-CMake users.
# just merge all contents into the file 'coroutine-config.cmake'
#
install(EXPORT ${PROJECT_NAME}-config
DESTINATION ${CMAKE_INSTALL_PREFIX}/share/${PROJECT_NAME}
)
#
# generate/install config & version info
#
include(CMakePackageConfigHelpers)
set(VERSION_FILE_PATH ${CMAKE_BINARY_DIR}/cmake/${PROJECT_NAME}-config-version.cmake)
write_basic_package_version_file(${VERSION_FILE_PATH}
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion
)
install(FILES ${VERSION_FILE_PATH}
DESTINATION ${CMAKE_INSTALL_PREFIX}/share/${PROJECT_NAME}
)
#
# for testing, CTest will be used
#
if(NOT BUILD_TESTING)
message(STATUS "Test is disabled.")
return()
elseif(NOT CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
message(STATUS "This is not a root project. Skipping the tests")
return()
elseif(ANDROID OR IOS)
return()
endif()
enable_testing()
# helper for test codes
set(BUILD_TESTING OFF)
add_subdirectory(external/latch)
add_subdirectory(external/sockets)
set(CMAKE_CXX_STANDARD 20)
# create_ctest( ... )
function(create_ctest TEST_NAME)
# create a test exe with the given name ...
add_executable(${TEST_NAME} test/${TEST_NAME}.cpp)
add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME})
target_link_libraries(${TEST_NAME}
PRIVATE
Microsoft.GSL::GSL
)
# all arguments after TEST_NAME
# should be library (or CMake target) name
foreach(idx RANGE 1 ${ARGC})
target_link_libraries(${TEST_NAME}
PRIVATE
${ARGV${idx}}
)
if("${ARGV${idx}}" STREQUAL ssf)
target_include_directories(${TEST_NAME}
PRIVATE
${PROJECT_SOURCE_DIR}/external/sockets
)
endif()
endforeach()
if(WIN32)
target_compile_definitions(${TEST_NAME}
PRIVATE
WIN32_LEAN_AND_MEAN NOMINMAX
)
elseif(APPLE)
target_link_libraries(${TEST_NAME}
PRIVATE
c++
)
else(CMAKE_SYSTEM_NAME MATCHES Linux)
target_link_libraries(${TEST_NAME}
PRIVATE
stdc++ c++
)
endif()
endfunction()
create_ctest( article_russian_roulette coroutine_portable )
#
# <coroutine/yield.hpp>
#
create_ctest( enumerable_accumulate coroutine_portable )
create_ctest( enumerable_iterator coroutine_portable )
create_ctest( enumerable_max_element coroutine_portable )
create_ctest( enumerable_move coroutine_portable )
create_ctest( enumerable_yield_never coroutine_portable )
create_ctest( enumerable_yield_once coroutine_portable )
create_ctest( enumerable_yield_rvalue coroutine_portable )
#
# <coroutine/frame.h>
# <coroutine/return.h>
#
create_ctest( return_destroy_with_handle coroutine_portable )
create_ctest( return_destroy_with_return coroutine_portable )
create_ctest( return_not_coroutine coroutine_portable )
create_ctest( return_not_subroutine coroutine_portable )
# create_ctest( return_std_future coroutine_portable )
#
# <coroutine/windows.h>
# <coroutine/unix.h>, <coroutine/linux.h>
# <coroutine/pthread.h>
#
if(TARGET coroutine_system)
if(WIN32)
create_ctest( windows_event_set coroutine_system )
create_ctest( windows_event_cancel coroutine_system )
create_ctest( windows_event_wait_one coroutine_system )
create_ctest( windows_event_wait_multiple coroutine_system )
create_ctest( windows_on_apc_known coroutine_system )
create_ctest( windows_on_apc_self coroutine_system )
create_ctest( windows_on_thread_pool coroutine_system latch )
elseif(UNIX)
if(CMAKE_CXX_COMPILER_ID MATCHES GNU)
# gcc-10 reports internal compiler error for these pthread tests
else()
create_ctest( pthread_detach_no_spawn coroutine_system )
create_ctest( pthread_detach_spawn coroutine_system )
create_ctest( pthread_join_no_spawn coroutine_system )
create_ctest( pthread_join_spawn_1 coroutine_system )
create_ctest( pthread_join_spawn_2 coroutine_system )
endif()
endif()
if(CMAKE_SYSTEM_NAME MATCHES Linux)
create_ctest( linux_event_no_wait coroutine_system )
create_ctest( linux_event_wait coroutine_system )
create_ctest( linux_event_signal coroutine_system )
elseif(UNIX)
create_ctest( unix_kqueue_single_thread coroutine_system )
endif()
endif()
#
# <coroutine/channel.hpp>
#
if(CMAKE_CXX_COMPILER_ID MATCHES GNU)
message(WARNING "using gcc: the compiler may not work for current code")
endif()
# create_ctest( channel_close_read coroutine_system )
# create_ctest( channel_close_write coroutine_system )
create_ctest( channel_ownership_consumer coroutine_system )
create_ctest( channel_ownership_producer coroutine_system )
create_ctest( channel_read_write_mutex coroutine_system )
create_ctest( channel_read_write_nolock coroutine_system )
create_ctest( channel_write_read_mutex coroutine_system )
create_ctest( channel_write_read_nolock coroutine_system )
# create_ctest( channel_select_empty coroutine_system )
# create_ctest( channel_select_type coroutine_system )
if(WIN32)
create_ctest( channel_race_condition coroutine_system latch )
endif()
create_ctest( channel_sample_wrap coroutine_system latch )
#
# <coroutine/net.h>
#
if(TARGET coroutine_net)
create_ctest( net_socket_tcp_echo coroutine_net ssf latch )
create_ctest( net_socket_udp_echo coroutine_net ssf latch )
create_ctest( net_resolve_name coroutine_net ssf )
create_ctest( net_resolve_ip6 coroutine_net ssf )
create_ctest( net_resolve_tcp6 coroutine_net ssf )
create_ctest( net_resolve_udp6 coroutine_net ssf )
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES Clang)
add_test(NAME test_clang_1 COMMAND ${CMAKE_CXX_COMPILER} --version)
endif()