Debian Codegraph @

library-vcpkg/CMakeLists.txt

raw
1 #
2 # D++ (DPP), The Lightweight C++ Discord Library
3 #
4 # Copyright 2021 Craig Edwards <[email protected]>
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18
19 add_compile_definitions(HAVE_VOICE)
20
21 file(GLOB THE_SOURCES "${DPP_ROOT_PATH}/src/dpp/events/*.cpp" "${DPP_ROOT_PATH}/src/dpp/voice/enabled/*.cpp" "${DPP_ROOT_PATH}/src/dpp/dave/*.cpp" "${DPP_ROOT_PATH}/src/dpp/cluster/*.cpp" "${DPP_ROOT_PATH}/src/dpp/*.cpp" "${DPP_ROOT_PATH}/src/dpp/*.rc")
22
23 set(LIB_NAME "${PROJECT_NAME}")
24
25 add_library("${LIB_NAME}" SHARED "${THE_SOURCES}")
26 if(NOT WIN32)
27 find_package(Threads REQUIRED)
28 endif()
29
30 include("${CMAKE_CURRENT_SOURCE_DIR}/../cmake/epoll.cmake")
31 include("${CMAKE_CURRENT_SOURCE_DIR}/../cmake/kqueue.cmake")
32 check_epoll(HAS_EPOLL)
33 check_kqueue(HAS_KQUEUE)
34 if (HAS_EPOLL)
35 message("-- Building with ${Green}epoll socket engine${ColourReset} -- ${Green}good!${ColourReset}")
36 target_sources("${LIB_NAME}" PRIVATE "${DPP_ROOT_PATH}/src/dpp/socketengines/epoll.cpp")
37 elseif (HAS_KQUEUE)
38 message("-- Building with ${Green}kqueue socket engine${ColourReset} -- ${Green}good!${ColourReset}")
39 target_sources("${LIB_NAME}" PRIVATE "${DPP_ROOT_PATH}/src/dpp/socketengines/kqueue.cpp")
40 else()
41 message("-- Building with ${Green}poll socket engine${ColourReset} -- ${Red}meh${ColourReset}!")
42 target_sources("${LIB_NAME}" PRIVATE "${DPP_ROOT_PATH}/src/dpp/socketengines/poll.cpp")
43 endif()
44
45
46 add_library("${PROJECT_NAME}::${LIB_NAME}" ALIAS "${LIB_NAME}")
47
48 if(${AVX_TYPE} STREQUAL "OFF")
49 include("${CMAKE_CURRENT_SOURCE_DIR}/../cmake/DetectArchitecture.cmake")
50 else()
51 message("-- AVX type overridden by configuration: ${AVX_TYPE}")
52 endif()
53 STRING(REPLACE "AVX" "" AVX_TYPE ${AVX_TYPE})
54 add_compile_definitions(AVX_TYPE=${AVX_TYPE})
55 add_compile_options(${AVX_FLAG})
56
57 target_compile_definitions(
58 "${LIB_NAME}" PUBLIC
59 "DPP_BUILD"
60 )
61
62 if(WIN32)
63 add_compile_definitions(OPENSSL_SYS_WIN32)
64 add_compile_definitions(_WINSOCK_DEPRECATED_NO_WARNINGS)
65 add_compile_definitions(WIN32_LEAN_AND_MEAN)
66 add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
67 add_compile_definitions(_CRT_NONSTDC_NO_DEPRECATE)
68 # Fake an ssl version number to satisfy MLSPP
69 set(OPENSSL_VERSION "1.1.1f")
70 endif()
71
72
73
74 target_compile_options(
75 "${LIB_NAME}"
76 PUBLIC
77 "$<$<PLATFORM_ID:Windows>:/bigobj;/Zc:preprocessor>"
78 PRIVATE
79 "$<$<PLATFORM_ID:Windows>:$<$<CONFIG:Debug>:/sdl;/Od;/DEBUG;/MP;/DFD_SETSIZE=1024>>"
80 "$<$<PLATFORM_ID:Windows>:$<$<CONFIG:Release>:/O2;/Oi;/Oy;/GL;/Gy;/sdl;/MP;/DFD_SETSIZE=1024>>"
81 "$<$<PLATFORM_ID:Linux>:$<$<CONFIG:Debug>:-fPIC;-Wall;-Wempty-body;-Wno-deprecated-declarations;-Wno-psabi;-Wunknown-pragmas;-Wignored-qualifiers;-Wimplicit-fallthrough;-Wmissing-field-initializers;-Wsign-compare;-Wtype-limits;-Wuninitialized;-Wshift-negative-value;-pthread;-g;-Og;-fPIC>>"
82 "$<$<PLATFORM_ID:Linux>:$<$<CONFIG:Release>:-fPIC;-Wall;-Wempty-body;-Wno-deprecated-declarations;-Wno-psabi;-Wunknown-pragmas;-Wignored-qualifiers;-Wimplicit-fallthrough;-Wmissing-field-initializers;-Wsign-compare;-Wtype-limits;-Wuninitialized;-Wshift-negative-value;-pthread;-O3;-fPIC>>"
83 "${AVX_FLAG}"
84 )
85
86 target_compile_features(
87 "${LIB_NAME}" PUBLIC
88 "cxx_std_20" "cxx_constexpr" "cxx_auto_type"
89 "cxx_defaulted_functions" "cxx_deleted_functions"
90 "cxx_final" "cxx_lambdas" "cxx_override" "cxx_thread_local"
91 "cxx_variadic_templates" "cxx_attribute_deprecated"
92 "cxx_enum_forward_declarations"
93 )
94
95 if(NOT DPP_NO_CORO)
96 message("-- Attempting to enable coroutines feature")
97 set(CMAKE_CXX_STANDARD 20)
98 target_compile_features(${LIB_NAME} PUBLIC cxx_std_20)
99 if(WIN32 AND NOT MINGW AND NOT DPP_CLANG_CL)
100 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /await:strict") # https://learn.microsoft.com/en-us/cpp/build/reference/await-enable-coroutine-support?view=msvc-170
101 if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
102 if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS_EQUAL 19.29.30158) # Taken from 2019 actions, as they seemingly fail to compile.
103 message("${BoldRed}Coroutines with MSVC (Visual Studio) require VS 2022 (Compiler Ver: 19.29.30158) or above. Forcing coroutines off.${ColourReset}")
104 set(DPP_NO_CORO ON)
105 endif()
106 endif()
107 else()
108 if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
109 if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 14.0.0) # clang >= 14 has native support
110 message("-- ${Yellow}Clang < 14 - attempting to detect if using libc++ or stdc++${ColourReset}")
111 check_cxx_source_compiles("
112 #include <iostream>
113
114 int a =
115 #ifdef __GLIBCXX__
116 1;
117 #else
118 fgsfds;
119 #endif
120
121 int main(int argc, char* argv[])
122 {
123 return 0;
124 }
125 " IS_GLIBCXX)
126 if(IS_GLIBCXX)
127 if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 12.0.0)
128 message("${BoldRed}Clang with stdc++ and coroutines requires version 12.0.0 or above. Forcing coroutines off.${ColourReset}")
129 set(DPP_NO_CORO ON)
130 else()
131 message("-- ${Yellow}Detected stdc++ - enabling mock std::experimental namespace${ColourReset}")
132 target_compile_definitions(${LIB_NAME} PUBLIC "STDCORO_GLIBCXX_COMPAT" "DPP_CORO")
133 endif()
134 else()
135 message("-- ${Yellow}Detected libc++ - using <experimental/coroutine>${ColourReset}")
136 if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0.0)
137 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fcoroutines-ts")
138 endif()
139 target_compile_definitions(${LIB_NAME} PUBLIC "STDCORO_GLIBCXX_COMPAT" "DPP_CORO")
140 endif()
141 message("-- ${Yellow}Note - coroutines in clang < 14 are experimental, upgrading is recommended${ColourReset}")
142 endif()
143 elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
144 if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 10.0)
145 message("${BoldRed}Coroutines with g++ require version 10 or above. Forcing coroutines off.${ColourReset}")
146 set(DPP_NO_CORO ON)
147 elseif(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 11.0)
148 message("-- ${Yellow}Note - coroutines in g++10 are experimental, upgrading to g++11 or above is recommended${ColourReset}")
149 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fcoroutines")
150 target_compile_definitions(${LIB_NAME} PUBLIC "STDCORO_GLIBCXX_COMPAT" "DPP_CORO")
151 endif()
152 endif()
153 endif()
154 endif()
155
156 if(DPP_NO_CORO)
157 message("-- ${Yellow}Coroutines are disabled.${ColourReset}")
158 target_compile_definitions(${LIB_NAME} PUBLIC DPP_NO_CORO)
159 else()
160 message("-- ${Green}Coroutines are enabled!${ColourReset}")
161 endif()
162
163 if(DPP_FORMATTERS)
164 target_compile_definitions(${LIB_NAME} PUBLIC DPP_FORMATTERS)
165 endif()
166
167 target_include_directories(
168 "${LIB_NAME}" PUBLIC
169 "$<BUILD_INTERFACE:${DPP_ROOT_PATH}/include>"
170 "$<INSTALL_INTERFACE:include>"
171 )
172
173 find_package(OpenSSL REQUIRED)
174 add_subdirectory("${DPP_ROOT_PATH}/mlspp" "mlspp")
175 include_directories("${DPP_ROOT_PATH}/mlspp/include")
176 include_directories("${DPP_ROOT_PATH}/mlspp/lib/bytes/include")
177 include_directories("${DPP_ROOT_PATH}/mlspp/lib/hpke/include")
178
179 set_target_properties(
180 "${LIB_NAME}" PROPERTIES
181 OUTPUT_NAME "dpp"
182 CXX_STANDARD_REQUIRED ON
183 )
184
185 target_link_options(
186 "${LIB_NAME}" PUBLIC
187 "$<$<PLATFORM_ID:Windows>:$<$<CONFIG:Debug>:/DEBUG>>"
188 )
189
190 find_package(nlohmann_json CONFIG REQUIRED)
191 if (DPP_TEST_VCPKG)
192 include("${CMAKE_CURRENT_SOURCE_DIR}/../cmake/FindOpus.cmake")
193 find_package(Opus REQUIRED)
194 else()
195 find_package(Opus CONFIG REQUIRED)
196 endif()
197 find_package(ZLIB REQUIRED)
198
199 target_link_libraries(
200 "${LIB_NAME}" PUBLIC
201 $<$<TARGET_EXISTS:nlohmann_json::nlohmann_json>:nlohmann_json::nlohmann_json>
202 $<$<TARGET_EXISTS:OpenSSL::SSL>:OpenSSL::SSL>
203 $<$<TARGET_EXISTS:OpenSSL::Crypto>:OpenSSL::Crypto>
204 $<$<TARGET_EXISTS:Opus::opus>:Opus::opus>
205 $<$<TARGET_EXISTS:ZLIB::ZLIB>:ZLIB::ZLIB>
206 $<$<TARGET_EXISTS:Threads::Threads>:Threads::Threads>
207 )
208
209 # Private statically linked dependencies
210 target_link_libraries(
211 ${LIB_NAME} PRIVATE
212 mlspp
213 mls_vectors
214 bytes
215 tls_syntax
216 hpke
217 )
218
219 set(CONFIG_FILE_NAME "${PROJECT_NAME}Config.cmake")
220 set(EXPORTED_TARGETS_NAME "${PROJECT_NAME}Targets")
221 set(EXPORTED_TARGETS_FILE_NAME "${EXPORTED_TARGETS_NAME}.cmake")
222 set(EXPORTED_TARGETS_FILE_PATH "share/dpp/${EXPORTED_TARGETS_FILE_NAME}")
223 set(RELEASE_PDB_FILE_PATH "bin/dpp.pdb")
224 set(DEBUG_PDB_FILE_PATH "debug/bin/dpp.pdb")
225
226 include(CMakePackageConfigHelpers)
227 configure_package_config_file(
228 "${DPP_ROOT_PATH}/cmake/${CONFIG_FILE_NAME}.in"
229 "${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_FILE_NAME}"
230 INSTALL_DESTINATION "share/dpp"
231 PATH_VARS
232 EXPORTED_TARGETS_FILE_PATH
233 RELEASE_PDB_FILE_PATH
234 DEBUG_PDB_FILE_PATH
235 )
236
237 set(VERSION_FILE_NAME "${PROJECT_NAME}ConfigVersion.cmake")
238
239 write_basic_package_version_file(
240 "${CMAKE_CURRENT_BINARY_DIR}/${VERSION_FILE_NAME}"
241 VERSION "${PRODUCT_VERSION}"
242 COMPATIBILITY AnyNewerVersion
243 )
244
245 install(
246 DIRECTORY "${DPP_ROOT_PATH}/include/"
247 DESTINATION "include"
248 )
249
250 install(
251 FILES
252 "${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_FILE_NAME}"
253 "${CMAKE_CURRENT_BINARY_DIR}/${VERSION_FILE_NAME}"
254 DESTINATION "share/dpp"
255 )
256
257 install(
258 TARGETS "${LIB_NAME}"
259 EXPORT "${EXPORTED_TARGETS_NAME}"
260 RUNTIME DESTINATION "$<$<NOT:$<PLATFORM_ID:Linux>>:$<IF:$<CONFIG:Debug>,${DEBUG_PREFIX}bin,bin>>"
261 ARCHIVE DESTINATION "$<IF:$<CONFIG:Debug>,${DEBUG_PREFIX}lib,lib>"
262 )
263
264 install(
265 EXPORT "${EXPORTED_TARGETS_NAME}"
266 FILE "${EXPORTED_TARGETS_FILE_NAME}"
267 NAMESPACE "${PROJECT_NAME}::"
268 DESTINATION "share/dpp"
269 )
270
271 if (WIN32)
272 install(
273 FILES
274 "$<TARGET_PDB_FILE:${LIB_NAME}>"
275 DESTINATION "$<IF:$<CONFIG:Debug>,${DEBUG_PREFIX}bin,bin>"
276 OPTIONAL
277 )
278 endif()