forked from libsdl-org/SDL_net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
305 lines (267 loc) · 10.3 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
cmake_minimum_required(VERSION 3.16)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
# See release_checklist.md
set(MAJOR_VERSION 2)
set(MINOR_VERSION 3)
set(MICRO_VERSION 0)
set(SDL_REQUIRED_VERSION 2.0.4)
include(PrivateSdlFunctions)
sdl_calculate_derived_version_variables()
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)
message(FATAL_ERROR "Prevented in-tree built. Please create a build directory outside of the SDL_net source code and call cmake from there")
endif()
project(SDL2_net
LANGUAGES C
VERSION "${FULL_VERSION}"
)
message(STATUS "Configuring ${PROJECT_NAME} ${PROJECT_VERSION}")
if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
set(SDL2NET_ROOTPROJECT ON)
else()
set(SDL2NET_ROOTPROJECT OFF)
endif()
if(TARGET SDL2::SDL2test)
set(SDL2test_FOUND ON)
else()
find_package(SDL2test)
endif()
set(SDLNET_PKGCONFIG_PRIVATE_LIBS)
# Set defaults preventing destination file conflicts
set(SDL2NET_DEBUG_POSTFIX "d"
CACHE STRING "Name suffix for debug builds")
mark_as_advanced(SDL2NET_DEBUG_POSTFIX)
set(sdl2net_install_enableable ON)
if ((TARGET SDL2 OR TARGET SDL2-static) AND SDL2_DISABLE_INSTALL)
# Cannot install SDL2_net when SDL2 is built in same built, and is not installed.
set(sdl2net_install_enableable OFF)
endif()
include(CMakeDependentOption)
include(CMakePackageConfigHelpers)
include(GNUInstallDirs)
option(CMAKE_POSITION_INDEPENDENT_CODE "Build static libraries with -fPIC" ON)
option(BUILD_SHARED_LIBS "Build the library as a shared library" ON)
cmake_dependent_option(SDL2NET_INSTALL "Enable SDL2_net install target" ${SDL2NET_ROOTPROJECT} ${sdl2net_install_enableable} OFF)
cmake_dependent_option(SDL2NET_SAMPLES "Build SDL2_net samples" ${SDL2NET_ROOTPROJECT} "${SDL2test_FOUND}" OFF)
if(SDL2NET_SAMPLES)
find_package(SDL2test REQUIRED)
endif()
# Save BUILD_SHARED_LIBS variable
set(SDL2NET_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
if(SDL2NET_BUILD_SHARED_LIBS)
set(sdl2_net_export_name SDL2_net)
set(sdl2_net_install_name_infix shared)
set(sdl2_target_name SDL2::SDL2)
else()
set(sdl2_net_export_name SDL2_net-static)
set(sdl2_net_install_name_infix static)
set(sdl2_target_name SDL2::SDL2-static)
endif()
sdl_find_sdl2(${sdl2_target_name} ${SDL_REQUIRED_VERSION})
add_library(SDL2_net
src/SDLnet.c
src/SDLnetselect.c
src/SDLnetTCP.c
src/SDLnetUDP.c
)
add_library(SDL2_net::${sdl2_net_export_name} ALIAS SDL2_net)
target_include_directories(SDL2_net
PRIVATE
"${CMAKE_CURRENT_LIST_DIR}/src"
PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/SDL2>"
)
target_compile_definitions(SDL2_net PRIVATE
BUILD_SDL
SDL_BUILD_MAJOR_VERSION=${MAJOR_VERSION}
SDL_BUILD_MINOR_VERSION=${MINOR_VERSION}
SDL_BUILD_MICRO_VERSION=${MICRO_VERSION}
)
target_link_libraries(SDL2_net PRIVATE $<BUILD_INTERFACE:${sdl2_target_name}>)
if(WIN32)
if (MSVC)
target_compile_options(SDL2_net PRIVATE /W3 /wd4244)
endif()
target_compile_definitions(SDL2_net PRIVATE _WINSOCK_DEPRECATED_NO_WARNINGS)
target_link_libraries(SDL2_net PRIVATE ws2_32 iphlpapi)
list(APPEND SDLNET_PKGCONFIG_PRIVATE_LIBS -lws2_32 -liphlpapi)
if(SDL2NET_BUILD_SHARED_LIBS)
target_sources(SDL2_net PRIVATE src/version.rc)
endif()
endif()
set_target_properties(SDL2_net PROPERTIES
DEFINE_SYMBOL DLL_EXPORT
EXPORT_NAME ${sdl2_net_export_name}
C_VISIBILITY_PRESET "hidden"
)
if(NOT ANDROID)
set_target_properties(SDL2_net PROPERTIES
DEBUG_POSTFIX "${SDL2NET_DEBUG_POSTFIX}"
)
if(APPLE)
# the SOVERSION property corresponds to the compatibility version and VERSION corresponds to the current version
# https://cmake.org/cmake/help/latest/prop_tgt/SOVERSION.html#mach-o-versions
set_target_properties(SDL2_net PROPERTIES
SOVERSION "${DYLIB_COMPATIBILITY_VERSION}"
VERSION "${DYLIB_CURRENT_VERSION}"
)
else()
set_target_properties(SDL2_net PROPERTIES
SOVERSION "${LT_MAJOR}"
VERSION "${LT_VERSION}"
)
endif()
endif()
if(SDL2NET_BUILD_SHARED_LIBS AND (APPLE OR (UNIX AND NOT ANDROID)))
add_custom_command(TARGET SDL2_net POST_BUILD
COMMAND "${CMAKE_COMMAND}" -E create_symlink "$<TARGET_SONAME_FILE_NAME:SDL2_net>" "libSDL2_net$<$<CONFIG:Debug>:${SDL2NET_DEBUG_POSTFIX}>$<TARGET_FILE_SUFFIX:SDL2_net>"
WORKING_DIRECTORY "${PROJECT_BINARY_DIR}"
)
endif()
if(SDL2NET_BUILD_SHARED_LIBS)
if(WIN32 OR OS2)
set_target_properties(SDL2_net PROPERTIES
PREFIX ""
)
endif()
if(OS2)
# FIXME: OS/2 Makefile has a different LIBNAME
set_target_properties(SDL2_net PROPERTIES
OUTPUT_NAME "SDL2net"
)
elseif(UNIX AND NOT ANDROID)
set_target_properties(SDL2_net PROPERTIES
OUTPUT_NAME "SDL2_net-${LT_RELEASE}"
)
endif()
else()
if(MSVC OR (WATCOM AND (WIN32 OR OS2)))
set_target_properties(SDL2_net PROPERTIES
OUTPUT_NAME "SDL2_net-static"
)
endif()
endif()
# Use `Compatible Interface Properties` to ensure a shared SDL2_net is built with a shared SDL2
if(SDL2NET_BUILD_SHARED_LIBS)
set_target_properties(SDL2_net PROPERTIES
INTERFACE_SDL2_SHARED ${SDL2NET_BUILD_SHARED_LIBS}
COMPATIBLE_INTERFACE_BOOL SDL2_SHARED
)
endif()
if(SDL2NET_BUILD_SHARED_LIBS)
sdl_target_link_options_no_undefined(SDL2_net)
endif()
if(SDL2NET_INSTALL)
install(
TARGETS SDL2_net
EXPORT SDL2NetExports
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/SDL_net.h"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/SDL2" COMPONENT devel
)
if(WIN32 AND NOT MINGW)
set(SDLNET_INSTALL_CMAKEDIR_DEFAULT "cmake")
else()
set(SDLNET_INSTALL_CMAKEDIR_DEFAULT "${CMAKE_INSTALL_LIBDIR}/cmake/SDL2_net")
endif()
set(SDLNET_INSTALL_CMAKEDIR "${SDLNET_INSTALL_CMAKEDIR_DEFAULT}" CACHE STRING "Location where to install SDL2_netConfig.cmake")
configure_package_config_file(
"${CMAKE_CURRENT_SOURCE_DIR}/SDL2_netConfig.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/SDL2_netConfig.cmake"
INSTALL_DESTINATION "${SDLNET_INSTALL_CMAKEDIR}"
)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/SDL2_netConfigVersion.cmake"
VERSION ${FULL_VERSION}
COMPATIBILITY AnyNewerVersion
)
install(
FILES
"${CMAKE_CURRENT_BINARY_DIR}/SDL2_netConfig.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/SDL2_netConfigVersion.cmake"
DESTINATION "${SDLNET_INSTALL_CMAKEDIR}"
COMPONENT devel
)
install(EXPORT SDL2NetExports
FILE SDL2_net-${sdl2_net_install_name_infix}-targets.cmake
NAMESPACE SDL2_net::
DESTINATION "${SDLNET_INSTALL_CMAKEDIR}"
COMPONENT devel
)
# Only generate build exports when SDL2 is not a subproject
if(NOT TARGET SDL2)
export(TARGETS SDL2_net
NAMESPACE SDL2_net::
FILE "SDL2_net-${sdl2_net_install_name_infix}-targets.cmake"
)
endif()
if(SDL2NET_BUILD_SHARED_LIBS)
set(ENABLE_SHARED_TRUE "")
set(ENABLE_SHARED_FALSE "#")
set(ENABLE_STATIC_TRUE "#")
set(ENABLE_STATIC_FALSE "")
else()
set(ENABLE_SHARED_TRUE "#")
set(ENABLE_SHARED_FALSE "")
set(ENABLE_STATIC_TRUE "")
set(ENABLE_STATIC_FALSE "#")
endif()
set(prefix "${CMAKE_INSTALL_PREFIX}")
set(exec_prefix "\${prefix}")
set(libdir "\${exec_prefix}/${CMAKE_INSTALL_LIBDIR}")
set(includedir "\${prefix}/${CMAKE_INSTALL_INCLUDEDIR}")
set(PACKAGE "${PROJECT_NAME}")
set(VERSION "${FULL_VERSION}")
set(SDL_VERSION "${SDL_REQUIRED_VERSION}")
string(JOIN " " PC_REQUIRES ${PC_REQUIRES})
string(JOIN " " PC_LIBS ${PC_LIBS})
string(JOIN " " INETLIB ${SDLNET_PKGCONFIG_PRIVATE_LIBS})
configure_file("${PROJECT_SOURCE_DIR}/SDL2_net.pc.in" "${CMAKE_CURRENT_BINARY_DIR}/SDL2_net.pc.intermediate" @ONLY)
file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/SDL2_net-$<CONFIG>.pc" INPUT "${CMAKE_CURRENT_BINARY_DIR}/SDL2_net.pc.intermediate")
set(PC_DESTDIR)
if(CMAKE_SYSTEM_NAME MATCHES FreeBSD)
# FreeBSD uses ${PREFIX}/libdata/pkgconfig
set(PC_DESTDIR "libdata/pkgconfig")
else()
set(PC_DESTDIR "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
endif()
# Always install SDL2_net.pc: libraries might be different between config modes
install(CODE "
# FIXME: use file(COPY_FILE) if minimum CMake version >= 3.21
execute_process(COMMAND \"\${CMAKE_COMMAND}\" -E copy_if_different
\"${CMAKE_CURRENT_BINARY_DIR}/SDL2_net-$<CONFIG>.pc\"
\"${CMAKE_CURRENT_BINARY_DIR}/SDL2_net.pc\")
file(INSTALL DESTINATION \"\${CMAKE_INSTALL_PREFIX}/${PC_DESTDIR}\"
TYPE FILE
FILES \"${CMAKE_CURRENT_BINARY_DIR}/SDL2_net.pc\")" COMPONENT devel)
if(SDL2NET_BUILD_SHARED_LIBS AND (APPLE OR (UNIX AND NOT ANDROID)))
install(
FILES
"${PROJECT_BINARY_DIR}/libSDL2_net$<$<CONFIG:Debug>:${SDL2NET_DEBUG_POSTFIX}>$<TARGET_FILE_SUFFIX:SDL2_net>"
DESTINATION "${CMAKE_INSTALL_LIBDIR}"
COMPONENT devel
)
endif()
install(FILES "LICENSE.txt"
DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/licenses/${PROJECT_NAME}"
COMPONENT library
)
endif()
if(SDL2NET_SAMPLES)
find_package(SDL2main)
add_executable(showinterfaces examples/showinterfaces.c)
target_compile_definitions(showinterfaces PRIVATE SDL_MAIN_HANDLED)
target_link_libraries(showinterfaces PRIVATE SDL2_net::${sdl2_net_export_name} ${sdl2_target_name})
add_executable(chat examples/chat.c examples/chat.h)
if(TARGET SDL2::SDL2main)
target_link_libraries(chat PRIVATE SDL2::SDL2main)
endif()
target_link_libraries(chat PRIVATE SDL2_net::${sdl2_net_export_name} SDL2::SDL2test ${sdl2_target_name})
add_executable(chatd examples/chatd.c examples/chat.h)
target_compile_definitions(chatd PRIVATE SDL_MAIN_HANDLED)
target_link_libraries(chatd PRIVATE SDL2_net::${sdl2_net_export_name} ${sdl2_target_name})
endif()