Skip to content

Commit 54caef2

Browse files
committed
chore: refine cmake
1 parent 185acc9 commit 54caef2

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

CMakeLists.txt

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,21 @@ else ()
1111
add_compile_options(-Wall -Wunused-parameter)
1212
endif ()
1313

14-
option(CORO_DISABLE_EXCEPTION "" OFF)
15-
option(CORO_ENABLE_LOCAL_STORAGE "" OFF)
14+
option(CORO_DISABLE_EXCEPTION "Disable C++ exceptions" OFF)
15+
option(CORO_ENABLE_LOCAL_STORAGE "Enable coroutine local storage" OFF)
1616

1717
add_library(${PROJECT_NAME} INTERFACE)
1818
target_include_directories(${PROJECT_NAME} INTERFACE include)
1919

2020
if (CORO_DISABLE_EXCEPTION)
21-
add_definitions(-DCORO_DISABLE_EXCEPTION)
21+
target_compile_definitions(${PROJECT_NAME} INTERFACE CORO_DISABLE_EXCEPTION)
2222
endif ()
2323
if (CORO_ENABLE_LOCAL_STORAGE)
24-
add_definitions(-CORO_ENABLE_LOCAL_STORAGE)
24+
target_compile_definitions(${PROJECT_NAME} INTERFACE CORO_ENABLE_LOCAL_STORAGE)
2525
endif ()
2626

27-
# test
28-
option(CORO_BUILD_TEST "" OFF)
27+
# Test
28+
option(CORO_BUILD_TEST "Build tests" OFF)
2929
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
3030
set(CORO_BUILD_TEST ON)
3131
endif ()
@@ -37,44 +37,43 @@ if (CORO_BUILD_TEST)
3737
# github actions runner of macOS is very slow
3838
if ($ENV{GITHUB_ACTIONS} AND $ENV{RUNNER_OS} STREQUAL "macOS")
3939
set(CORO_TEST_RUNNER_VERY_SLOW ON)
40-
add_definitions(-DCORO_TEST_RUNNER_VERY_SLOW)
40+
add_compile_definitions(CORO_TEST_RUNNER_VERY_SLOW)
4141
endif ()
4242

4343
if (CORO_ENABLE_SANITIZE_ADDRESS)
44-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
45-
set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
44+
add_compile_options(-fsanitize=address)
45+
add_link_options(-fsanitize=address)
4646
endif ()
4747
if (CORO_ENABLE_SANITIZE_THREAD)
48-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=thread")
49-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread")
48+
add_compile_options(-fsanitize=thread)
49+
add_link_options(-fsanitize=thread)
5050
endif ()
51+
5152
if (CORO_DISABLE_EXCEPTION)
5253
if (MSVC)
5354
# MSVC: Remove default exception handling flags and add disable flags
5455
string(REPLACE "/EHsc" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
5556
string(REPLACE "/EHs" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
5657
string(REPLACE "/EHc" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
5758
add_compile_options(/EHs-c-)
58-
add_definitions(-D_HAS_EXCEPTIONS=0)
59+
add_compile_definitions(_HAS_EXCEPTIONS=0)
5960
else ()
6061
# GCC/Clang
6162
add_compile_options(-fno-exceptions)
6263
endif ()
6364
endif ()
6465

6566
include_directories(test/detail)
66-
67-
file(GLOB SRCS test/coro_task.cpp)
6867
link_libraries(${PROJECT_NAME})
6968

7069
# test task
7170
set(TARGET_NAME ${PROJECT_NAME}_task)
72-
add_executable(${TARGET_NAME} ${SRCS})
71+
add_executable(${TARGET_NAME} test/coro_task.cpp)
7372

7473
# test task verbose
7574
set(TARGET_NAME ${PROJECT_NAME}_task_verbose)
76-
add_executable(${TARGET_NAME} ${SRCS})
77-
target_compile_definitions(${TARGET_NAME} PUBLIC -DCORO_DEBUG_LEAK_LOG=LOG -DCORO_DEBUG_LIFECYCLE=LOG)
75+
add_executable(${TARGET_NAME} test/coro_task.cpp)
76+
target_compile_definitions(${TARGET_NAME} PUBLIC CORO_DEBUG_LEAK_LOG=LOG CORO_DEBUG_LIFECYCLE=LOG)
7877

7978
# test mutex
8079
set(TARGET_NAME ${PROJECT_NAME}_mutex)
@@ -131,25 +130,24 @@ if (CORO_BUILD_TEST)
131130
# test multi thread(use single thread)
132131
set(TARGET_NAME ${PROJECT_NAME}_multi_thread_st)
133132
add_executable(${TARGET_NAME} test/coro_multi_thread.cpp)
134-
target_compile_definitions(${TARGET_NAME} PRIVATE -DCORO_USE_SINGLE_THREAD)
133+
target_compile_definitions(${TARGET_NAME} PRIVATE CORO_USE_SINGLE_THREAD)
135134

136135
# test coro_local (requires CORO_ENABLE_LOCAL_STORAGE)
137136
set(TARGET_NAME ${PROJECT_NAME}_coro_local)
138137
add_executable(${TARGET_NAME} test/coro_local.cpp)
139-
target_compile_definitions(${TARGET_NAME} PRIVATE -DCORO_ENABLE_LOCAL_STORAGE)
138+
target_compile_definitions(${TARGET_NAME} PRIVATE CORO_ENABLE_LOCAL_STORAGE)
140139

141140
message(STATUS "ASIO_PATH: $ENV{ASIO_PATH}")
142-
# test asio adaptor (optional, requires asio)
141+
# test asio adaptor (optional, requires asio and exceptions enabled)
143142
set(TARGET_NAME ${PROJECT_NAME}_asio_adaptor)
144143
if (NOT "$ENV{ASIO_PATH}" STREQUAL "" AND NOT CORO_DISABLE_EXCEPTION)
145-
if (MINGW)
146-
add_compile_options(-D_WIN32_WINNT=0x0601)
147-
link_libraries(ws2_32 wsock32)
148-
endif ()
149-
150-
add_definitions(-DASIO_NO_DEPRECATED)
151144
add_executable(${TARGET_NAME} test/coro_asio_adaptor.cpp)
152145
target_include_directories(${TARGET_NAME} PRIVATE $ENV{ASIO_PATH})
146+
target_compile_definitions(${TARGET_NAME} PRIVATE ASIO_NO_DEPRECATED)
147+
if (MINGW)
148+
target_compile_definitions(${TARGET_NAME} PRIVATE _WIN32_WINNT=0x0601)
149+
target_link_libraries(${TARGET_NAME} PRIVATE ws2_32 wsock32)
150+
endif ()
153151
else ()
154152
add_executable(${TARGET_NAME} test/coro_empty.cpp)
155153
endif ()

0 commit comments

Comments
 (0)