-
Notifications
You must be signed in to change notification settings - Fork 50
/
CMakeLists.txt
271 lines (235 loc) · 9.86 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
cmake_minimum_required(VERSION 3.16)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
# See docs/release_checklist.md
set(MAJOR_VERSION 3)
set(MINOR_VERSION 0)
set(MICRO_VERSION 0)
set(SDL_REQUIRED_VERSION 3.0.0)
project(SDL3_net
LANGUAGES C
VERSION "${MAJOR_VERSION}.${MINOR_VERSION}.${MICRO_VERSION}"
)
include("${SDL3_net_SOURCE_DIR}/cmake/GetGitRevisionDescription.cmake")
include("${SDL3_net_SOURCE_DIR}/cmake/PrivateSdlFunctions.cmake")
include("${SDL3_net_SOURCE_DIR}/cmake/sdlmanpages.cmake")
sdl_calculate_derived_version_variables(${MAJOR_VERSION} ${MINOR_VERSION} ${MICRO_VERSION})
message(STATUS "Configuring ${PROJECT_NAME} ${PROJECT_VERSION}")
if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
set(SDLNET_ROOTPROJECT ON)
else()
set(SDLNET_ROOTPROJECT OFF)
endif()
set(SDLNET_SAMPLES_DEFAULT ${SDLNET_ROOTPROJECT})
if(ANDROID)
set(SDLNET_SAMPLES_DEFAULT OFF)
endif()
set(sdl3net_install_enableable ON)
if((TARGET SDL3-shared OR TARGET SDL3-static) AND SDL_DISABLE_INSTALL)
# Cannot install SDL3_net when SDL3 is built in same built, and is not installed.
set(sdl3net_install_enableable OFF)
endif()
if(NOT DEFINED CMAKE_FIND_PACKAGE_PREFER_CONFIG)
set(CMAKE_FIND_PACKAGE_PREFER_CONFIG ON)
endif()
include(CMakeDependentOption)
include(CMakePackageConfigHelpers)
include(GNUInstallDirs)
set(PLATFORM_SUPPORTS_SHARED ON)
if(VITA OR PSP OR PS2 OR N3DS OR RISCOS)
set(PLATFORM_SUPPORTS_SHARED OFF)
endif()
option(CMAKE_POSITION_INDEPENDENT_CODE "Build static libraries with -fPIC" ${PLATFORM_SUPPORTS_SHARED})
cmake_dependent_option(BUILD_SHARED_LIBS "Build the library as a shared library" ON PLATFORM_SUPPORTS_SHARED OFF)
cmake_dependent_option(SDLNET_INSTALL "Enable SDL3_net install target" ${SDLNET_ROOTPROJECT} "${sdl3net_install_enableable}" OFF)
cmake_dependent_option(SDLNET_INSTALL_MAN "Install man pages for SDL3_net" OFF "SDLNET_INSTALL" OFF)
option(SDLNET_WERROR "Treat warnings as errors" OFF)
option(SDLNET_SAMPLES "Build the SDL3_net sample program(s)" ${SDLNET_SAMPLES_DEFAULT})
cmake_dependent_option(SDLNET_SAMPLES_INSTALL "Install the SDL3_net sample program(s)" OFF "SDLNET_SAMPLES;SDLNET_INSTALL" OFF)
# Save BUILD_SHARED_LIBS variable
set(SDLNET_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
set(sdl_required_components Headers)
if(SDLNET_BUILD_SHARED_LIBS)
set(sdl3_net_target_name SDL3_net-shared)
set(sdl3_target_name SDL3::SDL3-shared)
list(APPEND sdl_required_components SDL3-shared)
else()
set(sdl3_net_target_name SDL3_net-static)
set(sdl3_target_name SDL3::SDL3)
endif()
if(NOT TARGET SDL3::Headers OR NOT TARGET ${sdl3_target_name})
find_package(SDL3 ${SDL_REQUIRED_VERSION} REQUIRED COMPONENTS ${sdl_required_components})
endif()
set(PC_LIBS)
set(PC_REQUIRES)
add_library(${sdl3_net_target_name} src/SDL_net.c)
add_library(SDL3_net::${sdl3_net_target_name} ALIAS ${sdl3_net_target_name})
set_property(TARGET ${sdl3_net_target_name} PROPERTY C_STANDARD 99)
if(NOT TARGET SDL3_net::SDL3_net)
add_library(SDL3_net::SDL3_net ALIAS ${sdl3_net_target_name})
endif()
target_include_directories(${sdl3_net_target_name}
PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
)
target_compile_definitions(${sdl3_net_target_name} PRIVATE
BUILD_SDL
SDL_BUILD_MAJOR_VERSION=${MAJOR_VERSION}
SDL_BUILD_MINOR_VERSION=${MINOR_VERSION}
SDL_BUILD_MICRO_VERSION=${MICRO_VERSION}
)
target_link_libraries(${sdl3_net_target_name} PUBLIC SDL3::Headers)
if(SDLNET_BUILD_SHARED_LIBS)
target_link_libraries(${sdl3_net_target_name} PRIVATE SDL3::SDL3-shared)
endif()
sdl_add_warning_options(${sdl3_net_target_name} WARNING_AS_ERROR ${SDLNET_WERROR})
if(WIN32)
if(SDLNET_BUILD_SHARED_LIBS)
target_sources(${sdl3_net_target_name} PRIVATE
src/version.rc
)
endif()
target_link_libraries(${sdl3_net_target_name} PRIVATE iphlpapi ws2_32)
list(APPEND PC_LIBS -liphlpapi -lws2_32)
endif()
if(CMAKE_SYSTEM_NAME MATCHES "Haiku.*")
target_link_libraries(${sdl3_net_target_name} PRIVATE network)
endif()
set_target_properties(${sdl3_net_target_name} PROPERTIES
OUTPUT_NAME "SDL3_net"
DEFINE_SYMBOL DLL_EXPORT
EXPORT_NAME ${sdl3_net_target_name}
C_VISIBILITY_PRESET "hidden"
)
sdl_target_link_option_version_file(${sdl3_net_target_name} "${CMAKE_CURRENT_SOURCE_DIR}/src/SDL_net.sym")
if(NOT ANDROID)
set_target_properties(${sdl3_net_target_name} PROPERTIES
SOVERSION "${SO_VERSION_MAJOR}"
VERSION "${SO_VERSION}"
)
if(APPLE)
cmake_minimum_required(VERSION 3.17)
set_target_properties(${sdl3_net_target_name} PROPERTIES
MACHO_COMPATIBILITY_VERSION "${DYLIB_COMPAT_VERSION}"
MACHO_CURRENT_VERSION "${DYLIB_CURRENT_VERSION}"
)
sdl_no_deprecated_errors(${sdl3_net_target_name})
endif()
endif()
if(SDLNET_BUILD_SHARED_LIBS)
if(WIN32)
set_target_properties(${sdl3_net_target_name} PROPERTIES
PREFIX ""
)
endif()
else()
if(MSVC)
set_target_properties(${sdl3_net_target_name} PROPERTIES
OUTPUT_NAME "SDL3_net-static"
)
endif()
endif()
# Use `Compatible Interface Properties` to ensure a shared SDL3_net is built with a shared SDL3
if(SDLNET_BUILD_SHARED_LIBS)
set_property(TARGET ${sdl3_net_target_name} PROPERTY INTERFACE_SDL3_SHARED TRUE)
set_property(TARGET ${sdl3_net_target_name} APPEND PROPERTY COMPATIBLE_INTERFACE_BOOL SDL3_SHARED)
endif()
if(SDLNET_BUILD_SHARED_LIBS)
sdl_target_link_options_no_undefined(${sdl3_net_target_name})
endif()
if(SDLNET_INSTALL)
install(
TARGETS ${sdl3_net_target_name}
EXPORT SDL3NetExports
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT devel
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT library
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT library
)
install(
FILES "${CMAKE_CURRENT_SOURCE_DIR}/include/SDL3_net/SDL_net.h"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/SDL3_net" COMPONENT devel
)
if(WIN32 AND NOT MINGW)
set(SDLNET_INSTALL_CMAKEDIR_ROOT_DEFAULT "cmake")
else()
set(SDLNET_INSTALL_CMAKEDIR_ROOT_DEFAULT "${CMAKE_INSTALL_LIBDIR}/cmake")
endif()
set(SDLNET_INSTALL_CMAKEDIR_ROOT "${SDLNET_INSTALL_CMAKEDIR_ROOT_DEFAULT}" CACHE STRING "Root folder where to install SDL3_netConfig.cmake related files (SDL3_net subfolder for MSVC projects)")
set(SDLNET_PKGCONFIG_INSTALLDIR "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
if(WIN32 AND NOT MINGW)
set(SDLNET_INSTALL_CMAKEDIR "${SDLNET_INSTALL_CMAKEDIR_ROOT}")
set(LICENSES_PREFIX "licenses/SDL3_net")
else()
set(SDLNET_INSTALL_CMAKEDIR "${SDLNET_INSTALL_CMAKEDIR_ROOT}/SDL3_net")
set(LICENSES_PREFIX "${CMAKE_INSTALL_DATAROOTDIR}/licenses/SDL3_net")
endif()
configure_package_config_file(cmake/SDL3_netConfig.cmake.in SDL3_netConfig.cmake
INSTALL_DESTINATION "${SDLNET_INSTALL_CMAKEDIR}"
)
write_basic_package_version_file("${PROJECT_BINARY_DIR}/SDL3_netConfigVersion.cmake"
COMPATIBILITY AnyNewerVersion
)
install(
FILES
"${CMAKE_CURRENT_BINARY_DIR}/SDL3_netConfig.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/SDL3_netConfigVersion.cmake"
DESTINATION "${SDLNET_INSTALL_CMAKEDIR}"
COMPONENT devel
)
install(EXPORT SDL3NetExports
FILE ${sdl3_net_target_name}-targets.cmake
NAMESPACE SDL3_net::
DESTINATION "${SDLNET_INSTALL_CMAKEDIR}"
COMPONENT devel
)
file(RELATIVE_PATH SDL_PATH_PREFIX_RELATIVE_TO_PKGCONFIG "${CMAKE_INSTALL_PREFIX}/${SDLNET_PKGCONFIG_INSTALLDIR}" "${CMAKE_INSTALL_PREFIX}")
string(REGEX REPLACE "[/]+$" "" SDL_PATH_PREFIX_RELATIVE_TO_PKGCONFIG "${SDL_PATH_PREFIX_RELATIVE_TO_PKGCONFIG}")
set(SDL_PKGCONFIG_PREFIX "\${pcfiledir}/${SDL_PATH_PREFIX_RELATIVE_TO_PKGCONFIG}")
string(JOIN " " PC_REQUIRES ${PC_REQUIRES})
string(JOIN " " PC_LIBS ${PC_LIBS})
configure_file(cmake/sdl3-net.pc.in sdl3-net.pc @ONLY)
# Always install sdl3-net.pc file: libraries might be different between config modes
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/sdl3-net.pc"
DESTINATION "${SDLNET_PKGCONFIG_INSTALLDIR}" COMPONENT devel)
if(BUILD_SHARED_LIBS)
set(pdbdir "${CMAKE_INSTALL_BINDIR}")
else()
set(pdbdir "${CMAKE_INSTALL_LIBDIR}")
endif()
if(MSVC)
SDL_install_pdb("${sdl3_net_target_name}" "${pdbdir}")
endif()
install(FILES "LICENSE.txt"
DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/licenses/${PROJECT_NAME}"
COMPONENT library
)
if(SDLNET_INSTALL_MAN)
sdl_get_git_revision_hash(SDLNET_REVISION)
SDL_generate_manpages(
HEADERS_DIR "${PROJECT_SOURCE_DIR}/include/SDL3_net"
SYMBOL "SDLNet_Init"
WIKIHEADERS_PL_PATH "${CMAKE_CURRENT_SOURCE_DIR}/build-scripts/wikiheaders.pl"
REVISION "${SDLNET_REVISION}"
)
endif()
endif()
if(SDLNET_SAMPLES)
add_executable(voipchat examples/voipchat.c)
add_executable(simple-http-get examples/simple-http-get.c)
add_executable(resolve-hostnames examples/resolve-hostnames.c)
add_executable(get-local-addrs examples/get-local-addrs.c)
set(examples voipchat simple-http-get resolve-hostnames get-local-addrs)
foreach(example IN LISTS examples)
target_link_libraries(${example} PRIVATE SDL3_net::SDL3_net SDL3::SDL3)
set_property(TARGET ${example} PROPERTY C_STANDARD 99)
set_property(TARGET ${example} PROPERTY C_EXTENSIONS FALSE)
sdl_add_warning_options(${example} WARNING_AS_ERROR ${SDLNET_WERROR})
if(SDLNET_SAMPLES_INSTALL)
install(TARGETS ${example}
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
)
endif()
endforeach()
# Build at least one example in C90
set_property(TARGET get-local-addrs PROPERTY C_STANDARD 90)
endif()