-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
257 lines (229 loc) · 8.17 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
cmake_minimum_required(VERSION 3.16)
# Copyright 2023 Garrett Fairburn <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
project(CClosure
VERSION 1.2.0
DESCRIPTION "Thread-safe closures as first-class functions for C"
LANGUAGES C
)
# Handle pre-release version.
set(PRE_RELEASE FALSE)
if(PRE_RELEASE)
set(EXTENDED_PROJECT_VERSION "${CMAKE_PROJECT_VERSION}-pre")
else()
set(EXTENDED_PROJECT_VERSION "${CMAKE_PROJECT_VERSION}")
endif()
# Set cmake defaults.
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)
# Set build configuration defaults.
set(CMAKE_C_FLAGS_DEBUG "-O0 -g3" CACHE STRING "Debug build flags.")
set(CMAKE_C_FLAGS_RELEASE "-O2 -g0 -DNDEBUG" CACHE STRING "Release build flags.")
set(CMAKE_C_FLAGS_RELWITHDEBINFO "-O2 -g3" CACHE STRING "Release with debug info build flags.")
set(CMAKE_C_FLAGS_MINSIZEREL "-Os -g0 -DNDEBUG" CACHE STRING "Minimum size release build flags.")
include(CTest)
include(GenerateExportHeader)
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
# Define options.
set(BUILD_ARCH_OPTS "x86" "x86_64")
list(JOIN BUILD_ARCH_OPTS " " BUILD_ARCH_OPTS_STR)
set(BUILD_ARCH "x86_64" CACHE STRING "Target architecure: ${BUILD_ARCH_OPTS_STR}")
if(NOT "${BUILD_ARCH}" IN_LIST BUILD_ARCH_OPTS)
message(FATAL_ERROR
"\"${BUILD_ARCH}\" is not a supported architecture!\n"
"Supported architectures: ${BUILD_ARCH_OPTS_STR}"
)
endif()
set(BUILD_THREADING TRUE CACHE BOOL "Whether or not to build with multi-threading support")
set(CMAKE_INSTALL_CMAKEDIR
"${CMAKE_INSTALL_LIBDIR}/cmake"
CACHE STRING "Installation directory for cmake configuration files relative to CMAKE_INSTALL_PREFIX"
)
if(BUILD_THREADING)
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads)
if(CMAKE_USE_PTHREADS_INIT)
set(THREAD_PTHREADS TRUE)
else()
message(FATAL_ERROR
"Build with multi-threading support selected, but could not find pthreads!"
)
endif()
endif()
find_package(Doxygen)
# Add cclosure object library target.
add_library(cclosure OBJECT
"src/cclosure.c"
)
generate_export_header(cclosure
BASE_NAME CCLOSURE
EXPORT_FILE_NAME include/export.h
)
set_target_properties(cclosure PROPERTIES
C_VISIBILITY_PRESET hidden
CMAKE_VISIBILITY_INLINES_HIDDEN TRUE
POSITION_INDEPENDENT_CODE TRUE
)
target_include_directories(cclosure
PRIVATE
"${CMAKE_CURRENT_BINARY_DIR}/include"
PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
)
target_compile_options(cclosure
PRIVATE
-Wall -Wextra -Werror -Wfatal-errors
$<$<STREQUAL:${BUILD_ARCH},x86>:-m32>
$<$<STREQUAL:${CMAKE_C_COMPILER_ID},GNU>:-Wno-clobbered>
)
if(THREAD_PTHREADS)
target_compile_definitions(cclosure
PRIVATE THREAD_PTHREADS=1
)
endif()
# Add cclosure concrete library targets.
add_library(cclosure_static STATIC "$<TARGET_OBJECTS:cclosure>")
add_library(cclosure_shared SHARED "$<TARGET_OBJECTS:cclosure>")
get_target_property(CCLOSURE_INTERFACE_INCLUDE_DIRECTORIES cclosure INTERFACE_INCLUDE_DIRECTORIES)
set_target_properties(cclosure_static PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${CCLOSURE_INTERFACE_INCLUDE_DIRECTORIES}"
OUTPUT_NAME cclosure
)
set_target_properties(cclosure_shared PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${CCLOSURE_INTERFACE_INCLUDE_DIRECTORIES}"
OUTPUT_NAME cclosure
)
target_link_options(cclosure_shared
PRIVATE
-rdynamic
$<$<STREQUAL:${BUILD_ARCH},x86>:-m32>
)
if(THREAD_PTHREADS)
target_link_libraries(cclosure_static
PRIVATE Threads::Threads
)
target_link_libraries(cclosure_shared
PRIVATE Threads::Threads
)
endif()
# Doxygen.
if(DOXYGEN_FOUND)
set(DOXYGEN_PROJECT_NUMBER "${EXTENDED_PROJECT_VERSION}")
set(DOXYGEN_HAVE_DOT FALSE)
set(DOXYGEN_SHOW_INCLUDE_FILES FALSE)
set(DOXYGEN_TYPEDEF_HIDES_STRUCT TRUE)
set(DOXYGEN_ENABLE_PREPROCESSING FALSE)
set(DOXYGEN_STRIP_FROM_PATH "include")
doxygen_add_docs(docs "${CMAKE_CURRENT_SOURCE_DIR}/include")
endif()
# Installation.
install(TARGETS cclosure_static cclosure_shared
EXPORT ${PROJECT_NAME}-targets
)
install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/include/cclosure.h"
TYPE INCLUDE
)
install(EXPORT ${PROJECT_NAME}-targets
FILE ${PROJECT_NAME}Targets.cmake
NAMESPACE ${PROJECT_NAME}::
DESTINATION "${CMAKE_INSTALL_CMAKEDIR}/${PROJECT_NAME}"
)
export(EXPORT ${PROJECT_NAME}-targets
NAMESPACE ${PROJECT_NAME}::
FILE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Targets.cmake"
)
configure_package_config_file("${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_NAME}Config.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION "${CMAKE_INSTALL_CMAKEDIR}/${PROJECT_NAME}"
)
write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion
)
install(FILES
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
DESTINATION "${CMAKE_INSTALL_CMAKEDIR}/${PROJECT_NAME}"
)
# Testing.
if (BUILD_TESTING)
macro(make_test TEST_NAME TEST_SUITE)
set(TEST_TARGET "test_${TEST_SUITE}_${TEST_NAME}_runner")
add_executable("${TEST_TARGET}"
"${CMAKE_CURRENT_SOURCE_DIR}/tests/src/${TEST_SUITE}/${TEST_NAME}.c"
)
set_target_properties("${TEST_TARGET}" PROPERTIES
OUTPUT_NAME "TestRunners/${TEST_SUITE}/${TEST_NAME}"
)
target_include_directories("${TEST_TARGET}"
PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/tests/include"
)
target_compile_options("${TEST_TARGET}"
PRIVATE
-O0 -g3 -Wall -Wextra -Werror -Wfatal-errors
$<$<STREQUAL:${BUILD_ARCH},x86>:-m32>
$<$<STREQUAL:${CMAKE_C_COMPILER_ID},GNU>:-Wno-clobbered>
)
target_link_options("${TEST_TARGET}"
PRIVATE
-rdynamic
$<$<STREQUAL:${BUILD_ARCH},x86>:-m32>
)
target_link_libraries("${TEST_TARGET}" PRIVATE cclosure_shared)
add_test(NAME "test_${TEST_SUITE}_${TEST_NAME}" COMMAND "${TEST_TARGET}")
set_tests_properties("test_${TEST_SUITE}_${TEST_NAME}" PROPERTIES
TIMEOUT 8
)
endmacro()
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/TestRunners/common")
macro(make_common_test TEST_NAME)
make_test("${TEST_NAME}" common
)
endmacro()
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/TestRunners/threading")
macro(make_threading_test TEST_NAME)
if(THREAD_PTHREADS)
make_test("${TEST_NAME}" threading)
target_link_libraries("${TEST_TARGET}"
PRIVATE Threads::Threads
)
endif()
endmacro()
make_common_test(thread_type)
if(THREAD_PTHREADS)
target_compile_definitions("${TEST_TARGET}"
PRIVATE THREAD_TYPE=CCLOSURE_THREAD_PTHREADS
)
else()
target_compile_definitions("${TEST_TARGET}"
PRIVATE THREAD_TYPE=CCLOSURE_THREAD_NONE
)
endif()
make_common_test(basic_alloc)
make_common_test(basic_usage)
make_common_test(agg_return)
make_common_test(agg_pass)
make_common_test(vargs_pass)
make_common_test(get_env)
make_common_test(get_fcn)
make_common_test(live_free)
make_common_test(live_free_agg)
make_common_test(graceful_fail)
make_common_test(excessive_alloc)
make_threading_test(basic)
make_threading_test(excessive)
endif()