forked from brainboxdotcc/DPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
316 lines (261 loc) · 11.7 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#
# D++ (DPP), The Lightweight C++ Discord Library
#
# Copyright 2021 Craig Edwards <[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.
#
cmake_minimum_required (VERSION 3.12)
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
option(BUILD_VOICE_SUPPORT "Build voice support" ON)
option(RUN_LDCONFIG "Run ldconfig after installation" ON)
option(DPP_BUILD_TEST "Build the test program" ON)
add_compile_definitions(DPP_BUILD)
#if(DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
# set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" CACHE STRING "")
#endif()
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/include/dpp/version.h" version_h)
if(NOT version_h MATCHES "DPP_VERSION_SHORT ([0-9][0-9])([0-9][0-9])([0-9][0-9])")
message(FATAL_ERROR "Cannot get DPP_VERSION_SHORT from version.h")
endif()
math(EXPR DPP_VERSION_MAJOR ${CMAKE_MATCH_1})
math(EXPR DPP_VERSION_MINOR ${CMAKE_MATCH_2})
math(EXPR DPP_VERSION_PATCH ${CMAKE_MATCH_3})
string(CONCAT DPP_VERSION ${DPP_VERSION_MAJOR}.${DPP_VERSION_MINOR}.${DPP_VERSION_PATCH})
project(libdpp VERSION ${DPP_VERSION} LANGUAGES CXX)
if(WIN32 AND NOT MINGW)
if (NOT WINDOWS_32_BIT)
message("-- Building for windows with precompiled packaged dependencies")
#set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
set(ZLIB_LIBRARIES "${CMAKE_CURRENT_SOURCE_DIR}/win32/lib")
set(ZLIB_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/win32/include")
set(OPENSSL_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/win32/include")
set(OPENSSL_LIBRARIES "${CMAKE_CURRENT_SOURCE_DIR}/win32/lib")
ADD_DEFINITIONS(/bigobj)
link_libraries("${CMAKE_CURRENT_SOURCE_DIR}/win32/lib/libssl.lib")
link_libraries("${CMAKE_CURRENT_SOURCE_DIR}/win32/lib/libcrypto.lib")
link_libraries("${CMAKE_CURRENT_SOURCE_DIR}/win32/lib/zlib.lib")
link_libraries("${CMAKE_CURRENT_SOURCE_DIR}/win32/lib/libsodium.lib")
link_libraries("${CMAKE_CURRENT_SOURCE_DIR}/win32/lib/opus.lib")
set(OPUS_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/win32/include")
set(OPUS_LIBRARIES "${CMAKE_CURRENT_SOURCE_DIR}/win32/lib/opus.lib")
set(sodium_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/win32/include")
set(sodium_LIBRARY_DEBUG "${CMAKE_CURRENT_SOURCE_DIR}/win32/lib/libsodium.lib")
set(sodium_LIBRARY_RELEASE "${CMAKE_CURRENT_SOURCE_DIR}/win32/lib/libsodium.lib")
set(HAVE_OPUS_OPUS_H "${CMAKE_CURRENT_SOURCE_DIR}/win32/include/opus/opus.h")
set(OPUS_FOUND 1)
SET(sodium_VERSION_STRING "win32 bundled")
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/win32/include")
add_compile_definitions(OPENSSL_SYS_WIN32)
add_compile_definitions(_WINSOCK_DEPRECATED_NO_WARNINGS)
add_compile_definitions(WIN32_LEAN_AND_MEAN)
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
add_compile_definitions(_CRT_NONSTDC_NO_DEPRECATE)
endif()
endif()
if (UNIX)
message("-- Checking for ability to update autogenerated files")
execute_process(
COMMAND which php
RESULT_VARIABLE HAS_PHP
OUTPUT_QUIET
)
if (${HAS_PHP} STREQUAL "0")
message("-- Checking for update to autogenerated files")
# target for rebuild of cluster::*_sync() functions
execute_process(
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
COMMAND php buildtools/make_sync_struct.php
)
set_source_files_properties( "${CMAKE_CURRENT_SOURCE_DIR}/include/dpp/cluster_sync_calls.h" PROPERTIES GENERATED TRUE )
else()
message("-- Autogeneration is not available")
endif()
endif()
if(NOT BUILD_SHARED_LIBS)
if(UNIX)
message("-- Building static library.")
if(UNIX AND NOT APPLE)
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
endif()
set(OPENSSL_USE_STATIC_LIBS ON)
set(sodium_USE_STATIC_LIBS ON)
set(OPUS_USE_STATIC_LIBS TRUE)
else()
message(WARNING "-- Building of static library not supported on non UNIX systems.")
endif()
endif()
include("cmake/colour.cmake")
if (BUILD_VOICE_SUPPORT)
if (MINGW OR NOT WIN32)
include("cmake/FindSodium.cmake")
include("cmake/FindOpus.cmake")
endif()
if(DEFINED OPUS_FOUND)
if(DEFINED sodium_VERSION_STRING)
add_compile_definitions(HAVE_VOICE)
message("-- Sodium ${Green}${sodium_VERSION_STRING}${ColourReset}")
set(HAVE_VOICE 1)
endif()
endif()
if(HAVE_VOICE)
message("-- Detected ${Green}libsodium${ColourReset} and ${Green}libopus${ColourReset}. VOICE support will be ${Green}enabled${ColourReset}")
else(HAVE_VOICE)
message("-- Could not detect ${Green}libsodium${ColourReset} and/or ${Green}libopus${ColourReset}. VOICE support will be ${Red}disabled${ColourReset}")
endif(HAVE_VOICE)
else()
message("-- Voice support disabled by cmake option")
endif()
aux_source_directory("src" coresrc)
string(ASCII 27 Esc)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
if(MINGW OR NOT WIN32)
find_package(ZLIB REQUIRED)
endif(MINGW OR NOT WIN32)
if(APPLE)
if(CMAKE_APPLE_SILICON_PROCESSOR)
set(OPENSSL_ROOT_DIR "/opt/homebrew/opt/openssl")
else()
set(OPENSSL_ROOT_DIR "/usr/local/opt/openssl")
endif()
find_package(OpenSSL REQUIRED)
else()
if(MINGW OR NOT WIN32)
find_package(OpenSSL REQUIRED)
endif()
endif()
include_directories(${OPENSSL_INCLUDE_DIR} ${ZLIB_INCLUDE_DIRS})
find_package(Git QUIET)
if(NOT GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
message(FATAL_ERROR "You are using a git version of D++ but do not have git installed. Install git (not 'gh') and try again.")
endif()
if(EXISTS "${PROJECT_SOURCE_DIR}/.git")
message("-- Building ${Green}git${ColourReset} version. ${Green}Be aware git versions may be unstable!${ColourReset}")
else()
message("-- Building ${Green}stable${ColourReset} version ${Green}${DPP_VERSION}${ColourReset}")
endif()
if(UNIX OR MSYS)
find_program(LDCONFIG_EXECUTABLE "ldconfig")
endif()
if(MSVC)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++17 /Od /DEBUG /Zi /sdl /MP /DFD_SETSIZE=1024 /Zc:preprocessor")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /std:c++17 /O2 /Oi /Oy /GL /Gy /sdl /MP /DFD_SETSIZE=1024 /Zc:preprocessor")
endif()
string(REGEX REPLACE "/W[1|2|3|4]" "/W3" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
else()
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -Wall -Wempty-body -Wignored-qualifiers -Wimplicit-fallthrough -Wmissing-field-initializers -Wsign-compare -Wtype-limits -Wuninitialized -Wshift-negative-value -pthread -g -Og -fPIC")
else()
if (BUILD_O3)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -Wall -Wempty-body -Wignored-qualifiers -Wimplicit-fallthrough -Wmissing-field-initializers -Wsign-compare -Wtype-limits -Wuninitialized -Wshift-negative-value -pthread -O3 -fPIC")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -Wall -Wempty-body -Wignored-qualifiers -Wimplicit-fallthrough -Wmissing-field-initializers -Wsign-compare -Wtype-limits -Wuninitialized -Wshift-negative-value -pthread -Os -fPIC")
endif()
endif()
if (NOT MINGW)
add_link_options("-rdynamic")
endif ()
endif()
set(modules_dir "src")
file(GLOB subdirlist ${modules_dir}/dpp)
foreach (fullmodname ${subdirlist})
get_filename_component(modname ${fullmodname} NAME)
set (modsrc "")
file(GLOB modsrc "${modules_dir}/dpp/*.cpp" "${modules_dir}/dpp/events/*.cpp" "${modules_dir}/dpp/cluster/*.cpp")
if(BUILD_SHARED_LIBS)
add_library(${modname} SHARED ${modsrc})
else()
add_library(${modname} STATIC ${modsrc})
endif()
set_target_properties(${modname}
PROPERTIES
VERSION ${CMAKE_PROJECT_VERSION}
SOVERSION ${CMAKE_PROJECT_VERSION}
)
target_include_directories(${modname} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
)
if (WIN32 AND NOT MINGW)
if (NOT WINDOWS_32_BIT)
target_link_libraries(${modname} PUBLIC "${PROJECT_SOURCE_DIR}/win32/lib/libssl.lib"
"${PROJECT_SOURCE_DIR}/win32/lib/libcrypto.lib"
"${PROJECT_SOURCE_DIR}/win32/lib/libsodium.lib"
"${PROJECT_SOURCE_DIR}/win32/lib/opus.lib"
"${PROJECT_SOURCE_DIR}/win32/lib/zlib.lib")
else()
target_link_libraries(${modname} PUBLIC "${PROJECT_SOURCE_DIR}/win32/32/lib/libssl.lib"
"${PROJECT_SOURCE_DIR}/win32/32/lib/libcrypto.lib"
"${PROJECT_SOURCE_DIR}/win32/32/lib/libsodium.lib"
"${PROJECT_SOURCE_DIR}/win32/32/lib/opus.lib"
"${PROJECT_SOURCE_DIR}/win32/32/lib/zlib.lib")
endif()
else()
target_link_libraries(${modname} PUBLIC ${OPENSSL_CRYPTO_LIBRARY} ${OPENSSL_SSL_LIBRARY} ${ZLIB_LIBRARIES})
if (MINGW)
target_link_libraries(${modname} PUBLIC wsock32 ws2_32 crypt32)
endif ()
endif()
if (HAVE_VOICE)
target_link_libraries(${modname} PUBLIC ${sodium_LIBRARY_RELEASE} ${OPUS_LIBRARIES})
include_directories(${OPUS_INCLUDE_DIRS} ${sodium_INCLUDE_DIR})
endif()
endforeach()
target_compile_features(dpp PRIVATE cxx_std_17)
target_compile_features(dpp PRIVATE cxx_constexpr)
target_compile_features(dpp PRIVATE cxx_auto_type)
target_compile_features(dpp PRIVATE cxx_defaulted_functions)
target_compile_features(dpp PRIVATE cxx_deleted_functions)
target_compile_features(dpp PRIVATE cxx_final)
target_compile_features(dpp PRIVATE cxx_lambdas)
target_compile_features(dpp PRIVATE cxx_override)
target_compile_features(dpp PRIVATE cxx_thread_local)
target_compile_features(dpp PRIVATE cxx_variadic_templates)
target_compile_features(dpp PRIVATE cxx_attribute_deprecated)
if (DPP_BUILD_TEST)
add_executable(test ${coresrc})
target_compile_features(test PRIVATE cxx_std_17)
target_link_libraries(test PUBLIC ${modname})
endif()
if(WIN32 AND NOT MINGW)
if (NOT WINDOWS_32_BIT)
configure_file("${PROJECT_SOURCE_DIR}/win32/bin/zlib1.dll" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" COPYONLY)
configure_file("${PROJECT_SOURCE_DIR}/win32/bin/libcrypto-1_1-x64.dll" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" COPYONLY)
configure_file("${PROJECT_SOURCE_DIR}/win32/bin/libssl-1_1-x64.dll" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" COPYONLY)
configure_file("${PROJECT_SOURCE_DIR}/win32/bin/libsodium.dll" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" COPYONLY)
configure_file("${PROJECT_SOURCE_DIR}/win32/bin/opus.dll" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" COPYONLY)
else()
configure_file("${PROJECT_SOURCE_DIR}/win32/32/bin/zlib1.dll" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" COPYONLY)
configure_file("${PROJECT_SOURCE_DIR}/win32/32/bin/libcrypto-1_1.dll" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" COPYONLY)
configure_file("${PROJECT_SOURCE_DIR}/win32/32/bin/libssl-1_1.dll" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" COPYONLY)
configure_file("${PROJECT_SOURCE_DIR}/win32/32/bin/libsodium.dll" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" COPYONLY)
configure_file("${PROJECT_SOURCE_DIR}/win32/32/bin/opus.dll" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}" COPYONLY)
endif()
endif()
if(NOT WIN32)
# Installation
include(GNUInstallDirs)
install(TARGETS dpp LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(CODE "execute_process(COMMAND ${CMAKE_COMMAND} -DRUN_LDCONFIG=${RUN_LDCONFIG} -DLDCONFIG_EXECUTABLE=${LDCONFIG_EXECUTABLE} -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/PostInstall.cmake)")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/dpp.pc.in" "${CMAKE_BINARY_DIR}/dpp.pc" @ONLY)
install(FILES "${CMAKE_BINARY_DIR}/dpp.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
elseif(MINGW)
install(TARGETS dpp LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_PREFIX}/include)
endif()
include("cmake/CPackSetup.cmake") # Setup information for packaging and distribution
# CPack initialization for distribution
include(CPack)