-
Notifications
You must be signed in to change notification settings - Fork 131
/
CMakeLists.txt
389 lines (345 loc) · 15.4 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
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.1.5)
project(SDL3_ttf
LANGUAGES C
VERSION "${MAJOR_VERSION}.${MINOR_VERSION}.${MICRO_VERSION}"
)
include("${SDL3_ttf_SOURCE_DIR}/cmake/GetGitRevisionDescription.cmake")
include("${SDL3_ttf_SOURCE_DIR}/cmake/PrivateSdlFunctions.cmake")
include("${SDL3_ttf_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(SDLTTF_ROOTPROJECT ON)
else()
set(SDLTTF_ROOTPROJECT OFF)
endif()
# Assume MSVC projects don't have a package manager and need vendored dependencies (by default).
# Most other platforms have some kind of package manager.
# FIXME: consider a package manager such as conan/vcpkg instead of vendoring
if(ANDROID OR MSVC)
set(vendored_default TRUE)
else()
set(vendored_default FALSE)
endif()
set(sdl3ttf_install_enableable ON)
if ((TARGET SDL3-shared OR TARGET SDL3-static) AND SDL_DISABLE_INSTALL)
# Cannot install SDL3_ttf when SDL3 is built in same built, and is not installed.
set(sdl3ttf_install_enableable OFF)
endif()
include(CMakeDependentOption)
include(CMakePackageConfigHelpers)
include(GNUInstallDirs)
include(CheckSymbolExists)
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(SDLTTF_INSTALL "Enable SDL3_ttf install target" ${SDLTTF_ROOTPROJECT} "${sdl3ttf_install_enableable}" OFF)
cmake_dependent_option(SDLTTF_INSTALL_MAN "Install man pages for SDL3_ttf" OFF "SDLTTF_INSTALL" OFF)
option(SDLTTF_VENDORED "Use vendored third-party libraries" ${vendored_default})
option(SDLTTF_WERROR "Treat warnings as errors" OFF)
option(SDLTTF_SAMPLES "Build the SDL3_ttf sample program(s)" ${SDLTTF_ROOTPROJECT})
cmake_dependent_option(SDLTTF_SAMPLES_INSTALL "Install the SDL3_ttf sample program(s)" OFF "SDLTTF_SAMPLES;SDLTTF_INSTALL" OFF)
# For style consistency, create a SDLTTF_FREETYPE CMake variable. This variable is NOT configurable.
set(SDLTTF_FREETYPE ON)
set(SDLTTF_FREETYPE_VENDORED "${SDLTTF_VENDORED}")
option(SDLTTF_HARFBUZZ "Use harfbuzz to improve text shaping" OFF)
set(SDLTTF_HARFBUZZ_VENDORED "${SDLTTF_VENDORED}")
# Save BUILD_SHARED_LIBS variable
set(SDLTTF_BUILD_SHARED_LIBS "${BUILD_SHARED_LIBS}")
set(sdl_required_components Headers)
if(SDLTTF_BUILD_SHARED_LIBS)
set(sdl3_ttf_target_name SDL3_ttf-shared)
set(sdl3_target_name SDL3::SDL3-shared)
list(APPEND sdl_required_components SDL3-shared)
else()
set(sdl3_ttf_target_name SDL3_ttf-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()
# Enable large file support on 32-bit glibc, so that the vendored libraries
# can access files with large inode numbers
check_symbol_exists("__GLIBC__" "stdlib.h" LIBC_IS_GLIBC)
if (LIBC_IS_GLIBC AND CMAKE_SIZEOF_VOID_P EQUAL 4)
add_compile_definitions(_FILE_OFFSET_BITS=64)
endif()
add_library(${sdl3_ttf_target_name}
src/SDL_hashtable.c
src/SDL_renderer_textengine.c
src/SDL_surface_textengine.c
src/SDL_ttf.c
)
add_library(SDL3_ttf::${sdl3_ttf_target_name} ALIAS ${sdl3_ttf_target_name})
if(NOT TARGET SDL3_ttf::SDL3_ttf)
add_library(SDL3_ttf::SDL3_ttf ALIAS ${sdl3_ttf_target_name})
endif()
target_include_directories(${sdl3_ttf_target_name}
PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
)
target_compile_definitions(${sdl3_ttf_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_ttf_target_name} PUBLIC SDL3::Headers)
if(SDLTTF_BUILD_SHARED_LIBS)
target_link_libraries(${sdl3_ttf_target_name} PRIVATE SDL3::SDL3-shared)
endif()
sdl_add_warning_options(${sdl3_ttf_target_name} WARNING_AS_ERROR ${SDLTTF_WERROR})
if ("c_std_99" IN_LIST CMAKE_C_COMPILE_FEATURES)
target_compile_features(${sdl3_ttf_target_name} PRIVATE c_std_99)
else()
message(WARNING "target_compile_features does not know c_std_99 for C compiler")
endif()
if(WIN32 AND SDLTTF_BUILD_SHARED_LIBS)
target_sources(${sdl3_ttf_target_name} PRIVATE
src/version.rc
)
endif()
set_target_properties(${sdl3_ttf_target_name} PROPERTIES
OUTPUT_NAME SDL3_ttf
DEFINE_SYMBOL DLL_EXPORT
EXPORT_NAME ${sdl3_ttf_target_name}
C_VISIBILITY_PRESET "hidden"
)
if(NOT ANDROID)
set_target_properties(${sdl3_ttf_target_name} PROPERTIES
SOVERSION "${SO_VERSION_MAJOR}"
VERSION "${SO_VERSION}"
)
if(APPLE)
cmake_minimum_required(VERSION 3.17)
set_target_properties(${sdl3_ttf_target_name} PROPERTIES
MACHO_COMPATIBILITY_VERSION "${DYLIB_COMPAT_VERSION}"
MACHO_CURRENT_VERSION "${DYLIB_CURRENT_VERSION}"
)
endif()
endif()
if(SDLTTF_BUILD_SHARED_LIBS)
if(WIN32)
set_target_properties(${sdl3_ttf_target_name} PROPERTIES
PREFIX ""
)
endif()
else()
if(MSVC)
set_target_properties(${sdl3_ttf_target_name} PROPERTIES
OUTPUT_NAME "SDL3_ttf-static"
)
endif()
endif()
if(SDLTTF_BUILD_SHARED_LIBS)
# Use `Compatible Interface Properties` to ensure a shared SDL3_ttf is linked to a shared SDL3 library
set_property(TARGET ${sdl3_ttf_target_name} PROPERTY INTERFACE_SDL3_SHARED ${SDLTTF_BUILD_SHARED_LIBS})
set_property(TARGET ${sdl3_ttf_target_name} APPEND PROPERTY COMPATIBLE_INTERFACE_BOOL SDL3_SHARED)
endif()
if(SDLTTF_BUILD_SHARED_LIBS)
sdl_target_link_options_no_undefined(${sdl3_ttf_target_name})
endif()
sdl_target_link_option_version_file(${sdl3_ttf_target_name} "${CMAKE_CURRENT_SOURCE_DIR}/src/SDL_ttf.sym")
if(SDLTTF_BUILD_SHARED_LIBS)
# Make sure static library dependencies are built with -fPIC when building a shared SDL3_ttf
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
endif()
set(INSTALL_EXTRA_TARGETS)
set(PC_LIBS)
set(PC_REQUIRES)
# Build freetype and harfbuzz as a static library
set(BUILD_SHARED_LIBS OFF)
if(SDLTTF_HARFBUZZ)
if(SDLTTF_HARFBUZZ_VENDORED)
message(STATUS "${PROJECT_NAME}: Using vendored harfbuzz library")
# HB_BUILD_UTILS variable is used by harfbuzz
set(HB_BUILD_UTILS OFF CACHE BOOL "harfbuzz build utils" FORCE)
# SKIP_INSTALL_LIBRARIES variable is used by harfbuzz
set(SKIP_INSTALL_LIBRARIES ON CACHE BOOL "harfbuzz install option" FORCE)
# HB_HAVE_FREETYPE variable is used by harfbuzz
set(HB_HAVE_FREETYPE ${SDLTTF_FREETYPE} CACHE BOOL "harfbuzz freetype helpers" FORCE)
if(NOT EXISTS "${PROJECT_SOURCE_DIR}/external/harfbuzz/CMakeLists.txt")
message(FATAL_ERROR "No harfbuzz sources found. Install a harfbuzz development package or run the download script in the external folder.")
endif()
add_subdirectory(external/harfbuzz EXCLUDE_FROM_ALL)
# harfbuzz is a c++ project, enable c++ here to ensure linking to the c++ standard library
enable_language(CXX)
if(NOT TARGET harfbuzz::harfbuzz)
add_library(harfbuzz::harfbuzz ALIAS harfbuzz)
endif()
if(SDLTTF_BUILD_SHARED_LIBS)
target_link_libraries(${sdl3_ttf_target_name} PRIVATE harfbuzz::harfbuzz)
else()
target_sources(${sdl3_ttf_target_name} PRIVATE $<TARGET_OBJECTS:harfbuzz::harfbuzz>)
target_include_directories(${sdl3_ttf_target_name} PRIVATE $<TARGET_PROPERTY:harfbuzz::harfbuzz,INTERFACE_INCLUDE_DIRECTORIES>)
if(NOT MSVC)
find_package(Threads)
target_link_libraries(${sdl3_ttf_target_name} PRIVATE Threads::Threads)
endif()
endif()
else()
message(STATUS "${PROJECT_NAME}: Using system harfbuzz library")
find_package(harfbuzz REQUIRED)
list(APPEND PC_REQUIRES harfbuzz)
target_link_libraries(${sdl3_ttf_target_name} PRIVATE harfbuzz::harfbuzz)
endif()
target_compile_definitions(${sdl3_ttf_target_name} PRIVATE TTF_USE_HARFBUZZ=1)
endif()
if(SDLTTF_FREETYPE)
if(SDLTTF_FREETYPE_VENDORED)
message(STATUS "${PROJECT_NAME}: Using vendored freetype library")
# FT_DISABLE_ZLIB variable is used by freetype
set(FT_DISABLE_ZLIB ON CACHE BOOL "freetype zlib option")
# FT_DISABLE_BZIP2 variable is used by freetype
set(FT_DISABLE_BZIP2 ON CACHE BOOL "freetype bzip2 option")
# FT_DISABLE_PNG variable is used by freetype
set(FT_DISABLE_PNG ON CACHE BOOL "freetype png option")
# FT_DISABLE_BROTLI variable is used by freetype
set(FT_DISABLE_BROTLI ON CACHE BOOL "freetype option")
if(SDLTTF_HARFBUZZ)
# FT_DISABLE_HARFBUZZ variable is used by freetype
set(FT_DISABLE_HARFBUZZ OFF CACHE BOOL "freetype harfbuzz option" FORCE)
# FT_REQUIRE_HARFBUZZ variable is used by freetype
set(FT_REQUIRE_HARFBUZZ ON CACHE BOOL "freetype harfbuzz option" FORCE)
else()
# FT_DISABLE_HARFBUZZ variable is used by freetype
set(FT_DISABLE_HARFBUZZ ON CACHE BOOL "freetype harfbuzz option" FORCE)
# FT_REQUIRE_HARFBUZZ variable is used by freetype
set(FT_REQUIRE_HARFBUZZ OFF CACHE BOOL "freetype harfbuzz option" FORCE)
endif()
if(NOT EXISTS "${PROJECT_SOURCE_DIR}/external/freetype/CMakeLists.txt")
message(FATAL_ERROR "No freetype sources found. Install a freetype development package or run the download script in the external folder.")
endif()
add_subdirectory(external/freetype EXCLUDE_FROM_ALL)
if(NOT TARGET Freetype::Freetype)
add_library(Freetype::Freetype ALIAS freetype)
endif()
if(SDLTTF_BUILD_SHARED_LIBS)
target_link_libraries(${sdl3_ttf_target_name} PRIVATE Freetype::Freetype)
else()
target_sources(${sdl3_ttf_target_name} PRIVATE $<TARGET_OBJECTS:Freetype::Freetype>)
target_include_directories(${sdl3_ttf_target_name} PRIVATE $<TARGET_PROPERTY:Freetype::Freetype,INTERFACE_INCLUDE_DIRECTORIES>)
endif()
else()
message(STATUS "${PROJECT_NAME}: Using system freetype library")
find_package(Freetype REQUIRED)
list(APPEND PC_REQUIRES freetype2)
target_link_libraries(${sdl3_ttf_target_name} PRIVATE Freetype::Freetype)
endif()
endif()
# Restore BUILD_SHARED_LIBS variable
set(BUILD_SHARED_LIBS ${SDLTTF_BUILD_SHARED_LIBS})
if(SDLTTF_INSTALL)
install(
TARGETS ${sdl3_ttf_target_name}
EXPORT SDL3_ttfTargets
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_ttf/SDL_textengine.h"
"${CMAKE_CURRENT_SOURCE_DIR}/include/SDL3_ttf/SDL_ttf.h"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/SDL3_ttf" COMPONENT devel
)
if(BUILD_SHARED_LIBS)
set(pdbdir "${CMAKE_INSTALL_BINDIR}")
else()
set(pdbdir "${CMAKE_INSTALL_LIBDIR}")
endif()
if(MSVC)
SDL_install_pdb("${sdl3_ttf_target_name}" "${pdbdir}")
endif()
if(WIN32 AND NOT MINGW)
set(SDLTTF_INSTALL_CMAKEDIR_DEFAULT "cmake")
else()
set(SDLTTF_INSTALL_CMAKEDIR_DEFAULT "${CMAKE_INSTALL_LIBDIR}/cmake")
endif()
set(SDLTTF_INSTALL_CMAKEDIR_ROOT "${SDLTTF_INSTALL_CMAKEDIR_DEFAULT}" CACHE STRING "Location where to install SDL3_ttfConfig.cmake")
set(SDLTTF_PKGCONFIG_INSTALLDIR "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
if(WIN32 AND NOT MINGW)
set(SDLTTF_INSTALL_CMAKEDIR "${SDLTTF_INSTALL_CMAKEDIR_ROOT}")
set(LICENSES_PREFIX "licenses/SDL3_ttf")
else()
set(SDLTTF_INSTALL_CMAKEDIR "${SDLTTF_INSTALL_CMAKEDIR_ROOT}/SDL3_ttf")
set(LICENSES_PREFIX "${CMAKE_INSTALL_DATAROOTDIR}/licenses/SDL3_ttf")
endif()
configure_package_config_file(cmake/SDL3_ttfConfig.cmake.in SDL3_ttfConfig.cmake
INSTALL_DESTINATION "${SDLTTF_INSTALL_CMAKEDIR}"
)
write_basic_package_version_file("${PROJECT_BINARY_DIR}/SDL3_ttfConfigVersion.cmake"
COMPATIBILITY AnyNewerVersion
)
install(
FILES
"${CMAKE_CURRENT_BINARY_DIR}/SDL3_ttfConfig.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/SDL3_ttfConfigVersion.cmake"
DESTINATION ${SDLTTF_INSTALL_CMAKEDIR}
COMPONENT devel
)
install(EXPORT SDL3_ttfTargets
FILE ${sdl3_ttf_target_name}-targets.cmake
NAMESPACE SDL3_ttf::
DESTINATION "${SDLTTF_INSTALL_CMAKEDIR}"
COMPONENT devel
)
file(RELATIVE_PATH SDL_PATH_PREFIX_RELATIVE_TO_PKGCONFIG "${CMAKE_INSTALL_PREFIX}/${SDLTTF_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-ttf.pc.in sdl3-ttf.pc @ONLY)
# Always install sdl3-ttf.pc file: libraries might be different between config modes
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/sdl3-ttf.pc"
DESTINATION "${SDLTTF_PKGCONFIG_INSTALLDIR}" COMPONENT devel)
install(FILES "LICENSE.txt"
DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/licenses/${PROJECT_NAME}"
COMPONENT library
)
if(SDLTTF_INSTALL_MAN)
sdl_get_git_revision_hash(SDLTTF_REVISION)
SDL_generate_manpages(
HEADERS_DIR "${PROJECT_SOURCE_DIR}/include/SDL3_ttf"
SYMBOL "TTF_Init"
WIKIHEADERS_PL_PATH "${CMAKE_CURRENT_SOURCE_DIR}/build-scripts/wikiheaders.pl"
REVISION "${SDLTTF_REVISION}"
)
endif()
endif()
if(SDLTTF_SAMPLES)
add_executable(glfont examples/glfont.c)
add_executable(showfont examples/showfont.c examples/editbox.c)
add_executable(testapp examples/testapp.c)
set(OpenGL_GL_PREFERENCE GLVND)
find_package(OpenGL)
if(TARGET OpenGL::OpenGL)
target_compile_definitions(glfont PRIVATE HAVE_OPENGL)
target_link_libraries(glfont PRIVATE OpenGL::OpenGL)
elseif(TARGET OpenGL::GL)
target_compile_definitions(glfont PRIVATE HAVE_OPENGL)
target_link_libraries(glfont PRIVATE OpenGL::GL)
endif()
foreach(prog glfont showfont testapp)
sdl_add_warning_options(${prog} WARNING_AS_ERROR ${SDLTTF_WERROR})
target_link_libraries(${prog} PRIVATE SDL3_ttf::${sdl3_ttf_target_name})
target_link_libraries(${prog} PRIVATE ${sdl3_target_name})
if("c_std_99" IN_LIST CMAKE_C_COMPILE_FEATURES)
target_compile_features(${prog} PRIVATE c_std_99)
endif()
if(SDLTTF_SAMPLES_INSTALL)
install(TARGETS ${prog}
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
)
endif()
endforeach()
endif()