-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
164 lines (146 loc) · 4.78 KB
/
CMakeLists.txt
File metadata and controls
164 lines (146 loc) · 4.78 KB
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
cmake_minimum_required(VERSION 3.12.0)
project(sockpuppet
LANGUAGES CXX
VERSION 1.1.0 # sets sockpuppet_VERSION, sockpuppet_VERSION_MAJOR, sockpuppet_VERSION_MINOR, sockpuppet_VERSION_PATCH
)
if(${CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT})
# install to the build directory by default
set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH "" FORCE)
endif()
option(SOCKPUPPET_BUILD_TESTS "Build test executables" TRUE)
option(SOCKPUPPET_BUILD_EXAMPLES "Build example executables" TRUE)
option(SOCKPUPPET_BUILD_SHARED_LIBS "Build shared instead of static library" TRUE)
option(SOCKPUPPET_WITH_TLS "Build with support for encrypted connections using OpenSSL (or compatible)" FALSE)
set(BUILD_SHARED_LIBS ${SOCKPUPPET_BUILD_SHARED_LIBS})
if(${BUILD_SHARED_LIBS})
# auto-generate DLL exports for Windows
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
endif()
if(MINGW)
# allow MinGW to drop compatibility with legacy Windows
add_definitions(-D_WIN32_WINNT=0x0600)
endif()
set(SOCKPUPPET_PUBLIC_HEADERS
include/sockpuppet/address.h
include/sockpuppet/socket.h
include/sockpuppet/socket_async.h
include/sockpuppet/socket_buffered.h
)
# this is influenced by the BUILD_SHARED_LIBS setting
add_library(sockpuppet
src/address.cpp
src/address_impl.cpp
src/address_impl_unix.cpp
src/address_impl_win.cpp
src/address_impl.h
src/driver_impl.cpp
src/driver_impl.h
src/error_code.cpp
src/error_code_tls.cpp
src/error_code_unix.cpp
src/error_code_win.cpp
src/error_code.h
src/socket.cpp
src/socket_async.cpp
src/socket_async_impl.cpp
src/socket_async_impl.h
src/socket_buffered.cpp
src/socket_buffered_impl.cpp
src/socket_buffered_impl.h
src/socket_impl.cpp
src/socket_impl.h
src/socket_tls_impl.cpp
src/socket_tls_impl.h
src/ssl_guard.cpp
src/ssl_guard.h
src/todo_impl.cpp
src/todo_impl.h
src/wait.cpp
src/wait.h
src/winsock_guard.cpp
src/winsock_guard.h
${SOCKPUPPET_PUBLIC_HEADERS}
)
set_target_properties(sockpuppet PROPERTIES PUBLIC_HEADER "${SOCKPUPPET_PUBLIC_HEADERS}")
# internals and public includes depend on C++17
target_compile_features(sockpuppet PUBLIC cxx_std_17)
# set/forward include directory
target_include_directories(sockpuppet PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
if(${SOCKPUPPET_WITH_TLS})
# LibreSSL works fine as well, just had to remove version suffix from installed lib filenames
message("OPENSSL_ROOT_DIR points to: ${OPENSSL_ROOT_DIR}")
find_package(OpenSSL REQUIRED)
# add define for preprocessor switches in source files, public includes and export file
target_compile_definitions(sockpuppet PUBLIC SOCKPUPPET_WITH_TLS)
list(APPEND DEPENDENCIES
OpenSSL::SSL
OpenSSL::Crypto
$<$<PLATFORM_ID:Windows>:bcrypt>
$<$<PLATFORM_ID:Linux>:pthread>
)
endif()
if(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
# avoid some unwanted windows header symbols
target_compile_definitions(sockpuppet PRIVATE NOMINMAX NOCRYPT)
# link with WinSock
list(APPEND DEPENDENCIES ws2_32)
endif()
if(${BUILD_SHARED_LIBS})
target_link_libraries(sockpuppet PRIVATE ${DEPENDENCIES})
else()
target_link_libraries(sockpuppet PUBLIC ${DEPENDENCIES})
endif()
if(${SOCKPUPPET_BUILD_TESTS})
add_subdirectory(test)
endif()
if(${SOCKPUPPET_BUILD_EXAMPLES})
add_subdirectory(examples)
endif()
# install the library, includes and CMake package
include(CMakePackageConfigHelpers)
export(TARGETS sockpuppet FILE sockpuppet-targets.cmake)
install(EXPORT sockpuppet-targets DESTINATION lib/cmake/sockpuppet)
install(TARGETS sockpuppet
EXPORT sockpuppet-targets
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
PUBLIC_HEADER DESTINATION include/sockpuppet
)
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/sockpuppet-config-version.cmake
VERSION ${sockpuppet_VERSION}
COMPATIBILITY AnyNewerVersion
)
if(NOT ${BUILD_SHARED_LIBS} AND ${SOCKPUPPET_WITH_TLS})
configure_package_config_file(
cmake/sockpuppet-config.cmake.static-tls.in
${CMAKE_CURRENT_BINARY_DIR}/sockpuppet-config.cmake
INSTALL_DESTINATION lib/cmake/sockpuppet
)
else()
configure_package_config_file(
cmake/sockpuppet-config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/sockpuppet-config.cmake
INSTALL_DESTINATION lib/cmake/sockpuppet
)
endif()
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/sockpuppet-config-version.cmake
${CMAKE_CURRENT_BINARY_DIR}/sockpuppet-config.cmake
DESTINATION lib/cmake/sockpuppet
)
# install the demo project
if(${BUILD_SHARED_LIBS})
configure_file(examples/CMakeLists.txt.shared.in examples/CMakeLists.txt)
else()
configure_file(examples/CMakeLists.txt.static.in examples/CMakeLists.txt)
endif()
install(FILES
examples/sockpuppet_http_server.cpp
"${CMAKE_CURRENT_BINARY_DIR}/examples/CMakeLists.txt"
DESTINATION demo
)