-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
216 lines (174 loc) · 5.66 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
cmake_minimum_required(VERSION 3.18.0)
set(CGULL_STATIC_SUFFIX "_s" CACHE STRING "Library suffix for static modules.")
set(CGULL_SHARED_SUFFIX "" CACHE STRING "Library suffix for shared modules.")
set(CGULL_DEBUG_SUFFIX "d" CACHE STRING "Library suffix for debug modules.")
option(CGULL_BUILD_TESTS "Tells CMake to generate targets for unit tests." off)
option(CGULL_WITH_BOOST "Use boost." off)
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} ${CMAKE_CURRENT_BINARY_DIR})
project(
cgull
VERSION 0.1.0
DESCRIPTION "Async primitives for C++20 inspired by BlueBird.js"
HOMEPAGE_URL "https://github.com/Dalvin3000/cgull"
LANGUAGES CXX
)
include(GNUInstallDirs)
include(GenerateExportHeader)
macro(gather_sources SRC HDR PHD PGH)
file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS ${SRC})
file(GLOB_RECURSE HEADERS CONFIGURE_DEPENDS ${HDR})
file(GLOB PUBLIC_HEADERS CONFIGURE_DEPENDS ${PHD})
file(GLOB PUBLIC_GUTS_HEADERS CONFIGURE_DEPENDS ${PGH})
endmacro()
function(add_module_variation TARGET_NAME TYPE)
add_library(${TARGET_NAME} ${TYPE})
# suffix build-up
if(TYPE STREQUAL "SHARED")
set(suffix "${CGULL_SHARED_SUFFIX}")
else()
set(suffix "${CGULL_STATIC_SUFFIX}")
endif()
# sources
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/src PREFIX "Source Files" FILES ${SOURCES})
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/src PREFIX "Header Files" FILES ${HEADERS})
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/include/${PROJECT_NAME} PREFIX "Public Header Files" FILES ${PUBLIC_HEADERS})
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR}/include/${PROJECT_NAME} PREFIX "Public Header Files" FILES ${PUBLIC_GUTS_HEADERS})
target_sources(${TARGET_NAME}
PRIVATE
${SOURCES}
${HEADERS}
${PUBLIC_HEADERS}
${PUBLIC_GUTS_HEADERS}
)
# compiler/linker options and flags
target_compile_options(${TARGET_NAME}
PRIVATE
$<$<COMPILE_LANG_AND_ID:CXX,Clang,GNU>:
-Wunused-result
>
PUBLIC
$<$<COMPILE_LANG_AND_ID:CXX,Clang,GNU>:
-fcoroutines-ts
-fcxx-exceptions
-fexceptions
>
$<$<COMPILE_LANG_AND_ID:CXX,MSVC>:/await>
$<$<PLATFORM_ID:Windows>:/MP>
)
# custom props
set_target_properties(${TARGET_NAME}
PROPERTIES
OUTPUT_NAME "${PROJECT_NAME}${suffix}"
DEBUG_POSTFIX "${CGULL_DEBUG_SUFFIX}"
CXX_STANDARD_REQUIRED on
CXX_STANDARD 20
CXX_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN on
LANGUAGES CXX
LINKER_LANGUAGE CXX
VERSION "${PROJECT_VERSION}"
)
# links
target_link_libraries(${TARGET_NAME}
PRIVATE
Threads::Threads
)
# directories
target_include_directories(${TARGET_NAME}
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
# defines
if(TYPE STREQUAL "SHARED")
target_compile_definitions(${TARGET_NAME}
PUBLIC
CGULL_SHARED
)
endif()
# install rules
install(
FILES ${PUBLIC_HEADERS}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}
)
install(
FILES ${PUBLIC_GUTS_HEADERS}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}/guts
)
install(
TARGETS ${TARGET_NAME}
EXPORT ${PROJECT_NAME}Config
)
endfunction()
# external deps
find_package(Threads REQUIRED)
# targets
gather_sources(
"src/*.cpp"
"src/*.h"
"include/cgull/*.h"
"include/cgull/*.hpp"
"include/cgull/guts/*.h"
)
add_module_variation(static STATIC)
add_module_variation(shared SHARED)
# shared lib export
generate_export_header(shared
EXPORT_MACRO_NAME CGULL_API
NO_EXPORT_MACRO_NAME CGULL_LOCAL
DEPRECATED_MACRO_NAME CGULL_DEPRECATED
NO_DEPRECATED_MACRO_NAME CGULL_NO_DEPRECATED
EXPORT_FILE_NAME ${PROJECT_NAME}_export.h
)
install(
FILES ${PROJECT_BINARY_DIR}/${PROJECT_NAME}_export.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}
)
# install cmake rules
install(
EXPORT ${PROJECT_NAME}Config
NAMESPACE ${PROJECT_NAME}::
DESTINATION lib/cmake
EXPORT_LINK_INTERFACE_LIBRARIES
)
if(ANDROID)
install(EXPORT_ANDROID_MK ${PROJECT_NAME}Config DESTINATION share/ndk-modules)
endif()
# tests
if(CGULL_BUILD_TESTS)
find_package(GTest CONFIG REQUIRED)
find_package(Boost CONFIG)
set(PROJECT_NAME_TESTS ${PROJECT_NAME}-tests)
file(GLOB_RECURSE TEST_SOURCES CONFIGURE_DEPENDS tests/*.h tests/*.cpp)
add_executable(${PROJECT_NAME_TESTS}
${TEST_SOURCES}
)
target_include_directories(${PROJECT_NAME_TESTS}
PRIVATE
${GTEST_INCLUDE_DIRS}
)
target_compile_definitions(${PROJECT_NAME_TESTS}
PRIVATE
GTEST_LANG_CXX11=1
GTEST_STDLIB_CXX11=1
)
set_target_properties(${PROJECT_NAME_TESTS}
PROPERTIES
VS_DEBUGGER_WORKING_DIRECTORY "\$(OutDir)"
CXX_STANDARD_REQUIRED on
CXX_STANDARD 20
)
target_link_libraries(${PROJECT_NAME_TESTS}
PRIVATE
shared
GTest::gtest
)
if(TARGET Boost::Boost)
target_link_libraries(${PROJECT_NAME_TESTS} PRIVATE GTest::gtest)
endif()
add_test(
NAME ${PROJECT_NAME_TESTS}
COMMAND $<TARGET_FILE:${PROJECT_NAME_TESTS}>
)
endif()