-
Notifications
You must be signed in to change notification settings - Fork 1
/
CMakeLists.txt
256 lines (211 loc) · 8.98 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
cmake_minimum_required(VERSION 2.8)
project(libwpcp C)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_GENERATED_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/generated)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
set(CMAKE_MACOSX_RPATH ON)
set(CPACK_PACKAGE_NAME ${CMAKE_PROJECT_NAME})
set(CPACK_PACKAGE_VERSION_MAJOR "0")
set(CPACK_PACKAGE_VERSION_MINOR "1")
set(CPACK_PACKAGE_VERSION_PATCH "0")
set(CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}")
if (NOT CPACK_GENERATOR)
if (UNIX)
set(CPACK_GENERATOR "TGZ")
else ()
set(CPACK_GENERATOR "ZIP")
endif ()
endif ()
if (NOT CPACK_PACKAGE_FILE_NAME)
set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}")
endif ()
if (MSVC)
set(ADDITIONAL_COMPILER_FLAGS "/Wall /wd4820")
if (MSVC_VERSION LESS 1600)
set(ADDITIONAL_COMPILER_FLAGS "${ADDITIONAL_COMPILER_FLAGS} /wd4255")
include_directories(win32compat)
endif ()
if (NOT MSVC_VERSION LESS 1500)
set(ADDITIONAL_COMPILER_FLAGS "${ADDITIONAL_COMPILER_FLAGS} /MP")
endif ()
else ()
set(ADDITIONAL_LIBWEBSOCKETS_C_FLAGS "-fPIC")
set(ADDITIONAL_COMPILER_FLAGS "-fvisibility=hidden -std=c99 -W -Wall -Wextra -fPIC")
if (WPCP_GENERATE_COVERAGE)
set(ADDITIONAL_FLAGS_DEBUG "-fprofile-arcs -ftest-coverage")
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${ADDITIONAL_FLAGS_DEBUG}")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${ADDITIONAL_FLAGS_DEBUG}")
if (CMAKE_COMPILER_IS_GNUCC AND CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -lgcov")
else ()
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} --coverage")
endif ()
endif()
endif ()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${ADDITIONAL_COMPILER_FLAGS}")
option(WPCP_BUILD_CHECK "Build Check as ExternalProject" OFF)
option(WPCP_BUILD_LIBWEBSOCKETS "Build LibWebSockets as ExternalProject" OFF)
include(ExternalProject)
if (WPCP_BUILD_LIBWEBSOCKETS)
ExternalProject_Add(
libwebsockets
GIT_REPOSITORY git://git.libwebsockets.org/libwebsockets
GIT_TAG "v2.0.3"
CMAKE_ARGS "-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}" "-DCMAKE_TOOLCHAIN_FILE:PATH=${CMAKE_TOOLCHAIN_FILE}" "-DCMAKE_C_FLAGS:STRING=${ADDITIONAL_LIBWEBSOCKETS_C_FLAGS}" "-DCMAKE_PREFIX_PATH:PATH=${CMAKE_PREFIX_PATH}" "-DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>" "-DLWS_WITHOUT_EXTENSIONS=ON" "-DLWS_WITHOUT_TESTAPPS=ON" "-DLWS_WITH_SSL:BOOL=ON"
)
set(LIBWEBSOCKETS_TARGET_NAME websockets${CMAKE_STATIC_LIBRARY_SUFFIX})
add_library(websockets STATIC IMPORTED)
add_dependencies(websockets libwebsockets)
ExternalProject_Get_Property(libwebsockets INSTALL_DIR)
set_property(TARGET websockets PROPERTY IMPORTED_LOCATION ${INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}${LIBWEBSOCKETS_TARGET_NAME})
set(LIBWEBSOCKETS_INCLUDE_DIRS ${INSTALL_DIR}/include)
set(LIBWEBSOCKETS_LIBRARIES websockets)
find_package(OpenSSL REQUIRED)
list(APPEND LIBWEBSOCKETS_INCLUDE_DIRS ${OPENSSL_INCLUDE_DIR})
list(APPEND LIBWEBSOCKETS_LIBRARIES ${OPENSSL_LIBRARIES})
else ()
find_package(LibWebSockets)
if (NOT LIBWEBSOCKETS_FOUND)
message(FATAL_ERROR "Call cmake with '-DWPCP_BUILD_LIBWEBSOCKETS=ON' to build LibWebSockets as ExternalProject.")
endif ()
endif ()
if (WPCP_BUILD_CHECK)
ExternalProject_Add(
check-code
GIT_REPOSITORY git://github.com/libcheck/check
GIT_TAG "0.10.0"
CMAKE_ARGS "-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}" "-DCMAKE_TOOLCHAIN_FILE:PATH=${CMAKE_TOOLCHAIN_FILE}" "-DCMAKE_PREFIX_PATH:PATH=${CMAKE_PREFIX_PATH}" "-DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>"
BUILD_IN_SOURCE 1
)
add_library(check STATIC IMPORTED)
add_library(compat STATIC IMPORTED)
add_dependencies(check check-code)
ExternalProject_Get_Property(check-code INSTALL_DIR)
ExternalProject_Get_Property(check-code SOURCE_DIR)
set_property(TARGET check PROPERTY IMPORTED_LOCATION ${INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}check${CMAKE_STATIC_LIBRARY_SUFFIX})
set_property(TARGET compat PROPERTY IMPORTED_LOCATION ${INSTALL_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}compat${CMAKE_STATIC_LIBRARY_SUFFIX})
set(CHECK_INCLUDE_DIRS ${INSTALL_DIR}/include ${SOURCE_DIR})
set(CHECK_LIBRARIES check compat)
set(CHECK_FOUND TRUE)
else ()
find_package(Check)
find_package(Subunit)
if (NOT CHECK_FOUND)
message(WARNING "Call cmake with '-DWPCP_BUILD_CHECK=ON' to build Check as ExternalProject. Not performing tests.")
endif ()
if (SUBUNIT_FOUND)
list(APPEND CHECK_LIBRARIES ${SUBUNIT_LIBRARIES})
endif ()
endif ()
if (UNIX AND NOT APPLE)
list(APPEND CHECK_LIBRARIES m pthread rt)
endif ()
if (WPCP_GENERATE_COVERAGE)
add_definitions(-DWPCP_DISABLE_ASSERT)
endif ()
include_directories(${LIBWEBSOCKETS_INCLUDE_DIRS} include src)
set(WPCP_PUBLIC_HEADERS
include/wpcp.h
)
set(WPCP_PRIVATE_HEADERS
src/wpcp_cbor.h
src/wpcp_session.h
src/wpcp_util.h
)
set(WPCP_SOURCES
src/wpcp.c
src/wpcp_cbor.c
src/wpcp_session.c
)
set(WPCP_LWS_PUBLIC_HEADERS
include/wpcp_lws.h
include/wpcp_lws_main.h
)
set(WPCP_LWS_SOURCES
src/wpcp_lws.c
src/wpcp_lws_main.c
)
set(WPCP_TEST_FILES
test/test.h
test/test.c
test/test_cbor_read_buffer.c
test/test_cbor_write.c
test/test_cbor_write_buffer.c
test/test_session.c
test/test_wpcp.c
)
set(EXAMPLE_SOURCES
example/example.c
)
set(WPCP_LIBRARIES "")
if (WINCE)
list(APPEND WPCP_LIBRARIES ws2)
elseif (WIN32)
list(APPEND WPCP_LIBRARIES ws2_32)
endif ()
source_group("Headers Private" FILES ${WPCP_PRIVATE_HEADERS})
source_group("Headers Public" FILES ${WPCP_PUBLIC_HEADERS} ${WPCP_LWS_PUBLIC_HEADERS})
source_group("Sources" FILES ${WPCP_SOURCES} ${WPCP_LWS_SOURCES})
source_group("Tests" FILES ${WPCP_TEST_FILES})
add_library(wpcp SHARED ${WPCP_PUBLIC_HEADERS} ${WPCP_PRIVATE_HEADERS} ${WPCP_SOURCES})
set_property(TARGET wpcp PROPERTY COMPILE_DEFINITIONS WPCP_EXPORTS)
set_property(TARGET wpcp PROPERTY PUBLIC_HEADER ${WPCP_PUBLIC_HEADERS})
target_link_libraries(wpcp ${WPCP_LIBRARIES})
install(TARGETS wpcp
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
PUBLIC_HEADER DESTINATION include
)
add_library(wpcp-lws SHARED ${WPCP_LWS_PUBLIC_HEADERS} ${WPCP_LWS_SOURCES})
set_property(TARGET wpcp-lws PROPERTY COMPILE_DEFINITIONS WPCP_LWS_EXPORTS)
set_property(TARGET wpcp-lws PROPERTY PUBLIC_HEADER ${WPCP_LWS_PUBLIC_HEADERS})
target_link_libraries(wpcp-lws wpcp ${LIBWEBSOCKETS_LIBRARIES})
if (NOT WIN32)
target_link_libraries(wpcp-lws pthread)
endif ()
install(TARGETS wpcp-lws
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
PUBLIC_HEADER DESTINATION include
)
add_executable(example ${EXAMPLE_SOURCES})
target_link_libraries(example wpcp-lws)
if (CHECK_FOUND)
include_directories(${CHECK_INCLUDE_DIRS})
add_executable(test_wpcp ${WPCP_TEST_FILES} ${WPCP_PUBLIC_HEADERS} ${WPCP_PRIVATE_HEADERS} ${WPCP_SOURCES})
set_property(TARGET test_wpcp PROPERTY COMPILE_DEFINITIONS WPCP_STATIC WPCP_LWS_STATIC WPCP_USE_CUSTOM_ALLOCATOR)
target_link_libraries(test_wpcp ${CHECK_LIBRARIES} ${WPCP_LIBRARIES})
enable_testing()
add_test(NAME wpcp COMMAND test_wpcp)
endif ()
find_program(GENHTML_EXECUTABLE NAMES genhtml)
find_program(LCOV_EXECUTABLE NAMES lcov)
if (GENHTML_EXECUTABLE AND LCOV_EXECUTABLE)
add_custom_target(coverage_reset
COMMAND ${LCOV_EXECUTABLE} --directory ${CMAKE_BINARY_DIR} --zerocounters
COMMAND ${LCOV_EXECUTABLE} --directory ${CMAKE_BINARY_DIR} --capture --initial --output-file ${CMAKE_BINARY_DIR}/coverage_base.info
)
add_custom_target(coverage_generate
COMMAND ${CMAKE_COMMAND} -E remove_directory ${CMAKE_BINARY_DIR}/coverage
COMMAND ${LCOV_EXECUTABLE} --directory ${CMAKE_BINARY_DIR} --capture --output-file ${CMAKE_BINARY_DIR}/coverage_test.info
COMMAND lcov --extract ${CMAKE_BINARY_DIR}/coverage_test.info "${CMAKE_SOURCE_DIR}/src/*" --output-file ${CMAKE_BINARY_DIR}/coverage_test.info
COMMAND ${GENHTML_EXECUTABLE} --baseline-file ${CMAKE_BINARY_DIR}/coverage_base.info --title ${CMAKE_PROJECT_NAME} --legend --show-details --num-spaces 4 --output-directory ${CMAKE_BINARY_DIR}/coverage ${CMAKE_BINARY_DIR}/coverage_test.info
)
else ()
message(STATUS "Can't generate coverage report: genhtml and/or lcov was not found")
endif ()
find_program(CPPCHECK_EXECUTABLE NAMES cppcheck)
find_program(CPPCHECKHTMLREPORT_EXECUTABLE NAMES cppcheck-htmlreport)
if (CPPCHECK_EXECUTABLE AND CPPCHECKHTMLREPORT_EXECUTABLE)
add_custom_target(cppcheck
COMMAND ${CMAKE_COMMAND} -E remove_directory ${CMAKE_BINARY_DIR}/cppcheck
COMMAND ${CPPCHECK_EXECUTABLE} --enable=style --force -j 8 --verbose --xml ${CMAKE_SOURCE_DIR} 3>&1 1>&2 2>&3 | ${CPPCHECKHTMLREPORT_EXECUTABLE} --title=${CMAKE_PROJECT_NAME} --report-dir=${CMAKE_BINARY_DIR}/cppcheck
)
else ()
message(STATUS "Can't run cppchek: cppcheck and/or cppcheck-htmlreport was not found")
endif ()
include(CPack)