add a lib for rendering text in sdl2 and rules to make in makefile

This commit is contained in:
2026-01-21 14:45:11 -06:00
parent f3c0aafe2f
commit e7d9c013f4
1754 changed files with 850297 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
# Common variables for FindSDL2*.cmake modules
set(_inc_suffixes include)
set(_lib_suffixes)
if(MSVC)
if(CMAKE_SIZEOF_VOID_P EQUAL 4)
list(APPEND _lib_suffixes "lib/x86")
endif()
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
list(APPEND _lib_suffixes "lib/x64")
endif()
endif()
if(MINGW)
if(CMAKE_SIZEOF_VOID_P EQUAL 4)
list(APPEND _lib_suffixes "i686-w64-mingw32/lib")
list(APPEND _inc_suffixes "i686-w64-mingw32/include")
endif()
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
list(APPEND _lib_suffixes "x86_46-w64-mingw32/lib")
list(APPEND _inc_suffixes "x86_46-w64-mingw32/include")
endif()
endif()

View File

@@ -0,0 +1,47 @@
# FIXME: this should be provided by SDL2
include(FindPackageHandleStandardArgs)
include("${CMAKE_CURRENT_LIST_DIR}/CommonFindSDL2.cmake")
find_library(SDL2_LIBRARY
NAMES SDL2
HINTS ${SDL2_DIR} ENV SDL2_DIR
PATH_SUFFIXES ${_lib_suffixes}
)
find_path(SDL2_INCLUDE_DIR
NAMES SDL_haptic.h
PATH_SUFFIXES SDL2
HINTS ${SDL2_DIR} ENV SDL2_DIR
PATH_SUFFIXES ${_inc_suffixes}
)
set(SDL2_VERSION)
if(SDL2_INCLUDE_DIR)
file(READ "${SDL2_INCLUDE_DIR}/SDL_version.h" _sdl_version_h)
string(REGEX MATCH "#define[ \t]+SDL_MAJOR_VERSION[ \t]+([0-9]+)" _sdl2_major_re "${_sdl_version_h}")
set(_sdl2_major "${CMAKE_MATCH_1}")
string(REGEX MATCH "#define[ \t]+SDL_MINOR_VERSION[ \t]+([0-9]+)" _sdl2_minor_re "${_sdl_version_h}")
set(_sdl2_minor "${CMAKE_MATCH_1}")
string(REGEX MATCH "#define[ \t]+SDL_PATCHLEVEL[ \t]+([0-9]+)" _sdl2_patch_re "${_sdl_version_h}")
set(_sdl2_patch "${CMAKE_MATCH_1}")
if(_sdl2_major_re AND _sdl2_minor_re AND _sdl2_patch_re)
set(SDL2_VERSION "${_sdl2_major}.${_sdl2_minor}.${_sdl2_patch}")
endif()
endif()
find_package_handle_standard_args(PrivateSDL2
REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR
VERSION_VAR SDL2_VERSION
)
if(PrivateSDL2_FOUND)
if(NOT TARGET PrivateSDL2::PrivateSDL2)
add_library(PrivateSDL2::PrivateSDL2 UNKNOWN IMPORTED)
set_target_properties(PrivateSDL2::PrivateSDL2 PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${SDL2_INCLUDE_DIR}"
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
IMPORTED_LOCATION "${SDL2_LIBRARY}"
)
endif()
endif()

View File

@@ -0,0 +1,24 @@
# FIXME: this should be provided by SDL2
include(FindPackageHandleStandardArgs)
include("${CMAKE_CURRENT_LIST_DIR}/CommonFindSDL2.cmake")
find_library(SDL2_MAIN_LIBRARY
NAMES SDL2main
HINTS ${SDL2_DIR} ENV SDL2_DIR
PATH_SUFFIXES ${_lib_suffixes}
)
find_package_handle_standard_args(SDL2main
REQUIRED_VARS SDL2_MAIN_LIBRARY
)
if(SDL2main_FOUND)
if(NOT TARGET SDL2::SDL2main)
add_library(SDL2::SDL2main UNKNOWN IMPORTED)
set_target_properties(SDL2::SDL2main PROPERTIES
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
IMPORTED_LOCATION "${SDL2_MAIN_LIBRARY}"
)
endif()
endif()

View File

@@ -0,0 +1,36 @@
# Create our own Findharfbuzz.cmake module because harfbuzz-config.cmake,
# distributed along e.g. msys2-mingw64 does not support anything other then APPLE and UNIX.
include(FindPackageHandleStandardArgs)
find_library(harfbuzz_LIBRARY
NAMES harfbuzz
)
find_path(harfbuzz_INCLUDE_PATH
NAMES hb.h
PATH_SUFFIXES harfbuzz
)
set(harfbuzz_COMPILE_FLAGS "" CACHE STRING "Extra compile flags of harfbuzz")
set(harfbuzz_LINK_LIBRARIES "" CACHE STRING "Extra link libraries of harfbuzz")
set(harfbuzz_LINK_FLAGS "" CACHE STRING "Extra link flags of harfbuzz")
find_package_handle_standard_args(harfbuzz
REQUIRED_VARS harfbuzz_LIBRARY harfbuzz_INCLUDE_PATH
)
if (harfbuzz_FOUND)
if (NOT TARGET harfbuzz::harfbuzz)
add_library(harfbuzz::harfbuzz UNKNOWN IMPORTED)
set_target_properties(harfbuzz::harfbuzz PROPERTIES
IMPORTED_LOCATION "${harfbuzz_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${harfbuzz_INCLUDE_PATH}"
COMPILE_FLAGS "${harfbuzz_COMPILE_FLAGS}"
INTERFACE_LINK_LIBRARIES "${harfbuzz_LINK_LIBRARIES}"
INTERFACE_LINK_FLAGS "${harfbuzz_LINK_FLAGS}"
)
endif()
endif()

View File

@@ -0,0 +1,289 @@
# This file is shared amongst SDL_image/SDL_mixer/SDL_ttf
macro(sdl_calculate_derived_version_variables)
if(NOT DEFINED MAJOR_VERSION OR NOT DEFINED MINOR_VERSION OR NOT DEFINED MICRO_VERSION)
message(FATAL_ERROR "MAJOR_VERSION, MINOR_VERSION and MICRO_VERSION need to be defined")
endif()
set(FULL_VERSION "${MAJOR_VERSION}.${MINOR_VERSION}.${MICRO_VERSION}")
# Calculate a libtool-like version number
math(EXPR BINARY_AGE "${MINOR_VERSION} * 100 + ${MICRO_VERSION}")
math(EXPR IS_DEVELOPMENT "${MINOR_VERSION} % 2")
if(IS_DEVELOPMENT)
# Development branch, 2.5.1 -> libSDL2_XXXXX-2.0.so.0.501.0
set(INTERFACE_AGE 0)
else()
# Stable branch, 2.6.1 -> libSDL2_XXXXX-2.0.so.0.600.1
set(INTERFACE_AGE ${MICRO_VERSION})
endif()
# Increment this if there is an incompatible change - but if that happens,
# we should rename the library from SDL2 to SDL3, at which point this would
# reset to 0 anyway.
set(LT_MAJOR "0")
math(EXPR LT_AGE "${BINARY_AGE} - ${INTERFACE_AGE}")
math(EXPR LT_CURRENT "${LT_MAJOR} + ${LT_AGE}")
set(LT_REVISION "${INTERFACE_AGE}")
# For historical reasons, the library name redundantly includes the major
# version twice: libSDL2_XXXXX-2.0.so.0.
# TODO: in SDL 3, set the OUTPUT_NAME to plain SDL3_XXXXX, which will simplify
# it to libSDL2_XXXXX.so.0
set(LT_RELEASE "2.0")
set(LT_VERSION "${LT_MAJOR}.${LT_AGE}.${LT_REVISION}")
# The following should match the versions in the Xcode project file.
# Each version is 1 higher than you might expect, for compatibility
# with libtool: macOS ABI versioning is 1-based, unlike other platforms
# which are normally 0-based.
math(EXPR DYLIB_CURRENT_VERSION_MAJOR "${LT_MAJOR} + ${LT_AGE} + 1")
math(EXPR DYLIB_CURRENT_VERSION_MINOR "${LT_REVISION}")
set(DYLIB_CURRENT_VERSION "${DYLIB_CURRENT_VERSION_MAJOR}.${DYLIB_CURRENT_VERSION_MINOR}.0")
set(DYLIB_COMPATIBILITY_VERSION "${DYLIB_CURRENT_VERSION_MAJOR}.0.0")
endmacro()
macro(sdl_find_sdl2 TARGET VERSION)
if(NOT TARGET ${TARGET})
# FIXME: can't add REQUIRED since not all SDL2 installs ship SDL2ConfigVersion.cmake (or sdl2-config-version.cmake)
find_package(SDL2 ${VERSION} QUIET)
endif()
if(NOT TARGET ${TARGET})
# FIXME: can't add REQUIRED since not all SDL2 installs ship SDL2Config.cmake (or sdl2-config.cmake)
find_package(SDL2 QUIET)
if(SDL2_FOUND)
message(WARNING "Could not verify SDL2 version. Assuming SDL2 has version of at least ${VERSION}.")
endif()
endif()
# Use Private FindSDL2.cmake module to find SDL2 for installations where no SDL2Config.cmake is available,
# or for those installations where no target is generated.
if(NOT TARGET ${TARGET})
message(STATUS "Using private SDL2 find module")
find_package(PrivateSDL2 ${VERSION} REQUIRED)
add_library(${TARGET} INTERFACE IMPORTED)
set_target_properties(${TARGET} PROPERTIES
INTERFACE_LINK_LIBRARIES "PrivateSDL2::PrivateSDL2"
)
endif()
endmacro()
function(read_absolute_symlink DEST PATH)
file(READ_SYMLINK "${PATH}" p)
if(NOT IS_ABSOLUTE "${p}")
get_filename_component(pdir "${PATH}" DIRECTORY)
set(p "${pdir}/${p}")
endif()
get_filename_component(p "${p}" ABSOLUTE)
set("${DEST}" "${p}" PARENT_SCOPE)
endfunction()
function(win32_implib_identify_dll DEST IMPLIB)
cmake_parse_arguments(ARGS "NOTFATAL" "" "" ${ARGN})
if(CMAKE_DLLTOOL)
execute_process(
COMMAND "${CMAKE_DLLTOOL}" --identify "${IMPLIB}"
RESULT_VARIABLE retcode
OUTPUT_VARIABLE stdout
ERROR_VARIABLE stderr)
if(NOT retcode EQUAL 0)
if(NOT ARGS_NOTFATAL)
message(FATAL_ERROR "${CMAKE_DLLTOOL} failed.")
else()
set("${DEST}" "${DEST}-NOTFOUND" PARENT_SCOPE)
return()
endif()
endif()
string(STRIP "${stdout}" result)
set(${DEST} "${result}" PARENT_SCOPE)
elseif(MSVC)
get_filename_component(CMAKE_C_COMPILER_DIRECTORY "${CMAKE_C_COMPILER}" DIRECTORY CACHE)
find_program(CMAKE_DUMPBIN NAMES dumpbin PATHS "${CMAKE_C_COMPILER_DIRECTORY}")
if(CMAKE_DUMPBIN)
execute_process(
COMMAND "${CMAKE_DUMPBIN}" "-headers" "${IMPLIB}"
RESULT_VARIABLE retcode
OUTPUT_VARIABLE stdout
ERROR_VARIABLE stderr)
if(NOT retcode EQUAL 0)
if(NOT ARGS_NOTFATAL)
message(FATAL_ERROR "dumpbin failed.")
else()
set(${DEST} "${DEST}-NOTFOUND" PARENT_SCOPE)
return()
endif()
endif()
string(REGEX MATCH "DLL name[ ]+:[ ]+([^\n]+)\n" match "${stdout}")
if(NOT match)
if(NOT ARGS_NOTFATAL)
message(FATAL_ERROR "dumpbin did not find any associated dll for ${IMPLIB}.")
else()
set(${DEST} "${DEST}-NOTFOUND" PARENT_SCOPE)
return()
endif()
endif()
set(result "${CMAKE_MATCH_1}")
set(${DEST} "${result}" PARENT_SCOPE)
else()
message(FATAL_ERROR "Cannot find dumpbin, please set CMAKE_DUMPBIN cmake variable")
endif()
else()
if(NOT ARGS_NOTFATAL)
message(FATAL_ERROR "Don't know how to identify dll from import library. Set CMAKE_DLLTOOL (for mingw) or CMAKE_DUMPBIN (for MSVC)")
else()
set(${DEST} "${DEST}-NOTFOUND")
endif()
endif()
endfunction()
function(get_actual_target)
set(dst "${ARGV0}")
set(target "${${dst}}")
get_target_property(alias "${target}" ALIASED_TARGET)
while(alias)
set(target "${alias}")
get_target_property(alias "${target}" ALIASED_TARGET)
endwhile()
set("${dst}" "${target}" PARENT_SCOPE)
endfunction()
function(target_get_dynamic_library DEST TARGET)
set(result)
get_actual_target(TARGET)
if(WIN32)
# Use the target dll of the import library
set(props_to_check IMPORTED_IMPLIB)
if(CMAKE_BUILD_TYPE)
list(APPEND props_to_check IMPORTED_IMPLIB_${CMAKE_BUILD_TYPE})
endif()
list(APPEND props_to_check IMPORTED_LOCATION)
if(CMAKE_BUILD_TYPE)
list(APPEND props_to_check IMPORTED_LOCATION_${CMAKE_BUILD_TYPE})
endif()
foreach (config_type ${CMAKE_CONFIGURATION_TYPES} RELEASE DEBUG RELWITHDEBINFO MINSIZEREL)
list(APPEND props_to_check IMPORTED_IMPLIB_${config_type})
list(APPEND props_to_check IMPORTED_LOCATION_${config_type})
endforeach()
foreach(prop_to_check ${props_to_check})
if(NOT result)
get_target_property(propvalue "${TARGET}" ${prop_to_check})
if(propvalue AND EXISTS "${propvalue}")
win32_implib_identify_dll(result "${propvalue}" NOTFATAL)
endif()
endif()
endforeach()
else()
# 1. find the target library a file might be symbolic linking to
# 2. find all other files in the same folder that symolic link to it
# 3. sort all these files, and select the 1st item on Linux, and last on Macos
set(location_properties IMPORTED_LOCATION)
if(CMAKE_BUILD_TYPE)
list(APPEND location_properties IMPORTED_LOCATION_${CMAKE_BUILD_TYPE})
endif()
foreach (config_type ${CMAKE_CONFIGURATION_TYPES} RELEASE DEBUG RELWITHDEBINFO MINSIZEREL)
list(APPEND location_properties IMPORTED_LOCATION_${config_type})
endforeach()
if(APPLE)
set(valid_shared_library_regex "\\.[0-9]+\\.dylib$")
else()
set(valid_shared_library_regex "\\.so\\.([0-9.]+)?[0-9]")
endif()
foreach(location_property ${location_properties})
if(NOT result)
get_target_property(library_path "${TARGET}" ${location_property})
if(EXISTS "${library_path}")
get_filename_component(library_path "${library_path}" ABSOLUTE)
while (IS_SYMLINK "${library_path}")
read_absolute_symlink(library_path "${library_path}")
endwhile()
get_filename_component(libdir "${library_path}" DIRECTORY)
file(GLOB subfiles "${libdir}/*")
set(similar_files "${library_path}")
foreach(subfile ${subfiles})
if(IS_SYMLINK "${subfile}")
read_absolute_symlink(subfile_target "${subfile}")
while (IS_SYMLINK "${subfile_target}")
read_absolute_symlink(subfile_target "${subfile_target}")
endwhile()
get_filename_component(subfile_target "${subfile_target}" ABSOLUTE)
if(subfile_target STREQUAL library_path AND subfile MATCHES "${valid_shared_library_regex}")
list(APPEND similar_files "${subfile}")
endif()
endif()
endforeach()
list(SORT similar_files)
set(index 0)
if(APPLE)
list(LENGTH similar_files len)
math(EXPR index "${len}-1")
endif()
list(GET similar_files ${index} item)
get_filename_component(result "${item}" NAME)
endif()
endif()
endforeach()
endif()
if(result)
string(TOLOWER "${result}" result_lower)
if(WIN32 OR OS2)
if(NOT result_lower MATCHES ".*dll")
message(FATAL_ERROR "\"${result}\" is not a .dll library")
endif()
elseif(APPLE)
if(NOT result_lower MATCHES ".*dylib.*")
message(FATAL_ERROR "\"${result}\" is not a .dylib shared library")
endif()
else()
if(NOT result_lower MATCHES ".*so.*")
message(FATAL_ERROR "\"${result}\" is not a .so shared library")
endif()
endif()
else()
get_target_property(target_type ${TARGET} TYPE)
if(target_type MATCHES "SHARED_LIBRARY|MODULE_LIBRARY")
# OK
elseif(target_type MATCHES "STATIC_LIBRARY|OBJECT_LIBRARY|INTERFACE_LIBRARY|EXECUTABLE")
message(SEND_ERROR "${TARGET} is not a shared library, but has type=${target_type}")
else()
message(WARNING "Unable to extract dynamic library from target=${TARGET}, type=${target_type}.")
endif()
# TARGET_SONAME_FILE is not allowed for DLL target platforms.
if(WIN32)
set(result "$<TARGET_FILE_NAME:${TARGET}>")
else()
set(result "$<TARGET_SONAME_FILE_NAME:${TARGET}>")
endif()
endif()
set(${DEST} ${result} PARENT_SCOPE)
endfunction()
macro(sdl_check_project_in_subfolder relative_subfolder name vendored_option)
if(NOT EXISTS "${PROJECT_SOURCE_DIR}/${relative_subfolder}/CMakeLists.txt")
message(FATAL_ERROR "No cmake project for ${name} found in ${relative_subfolder}.\n"
"Run the download script in the external folder, or re-configure with -D${vendored_option}=OFF to use system packages.")
endif()
endmacro()
macro(sdl_check_linker_flag flag var)
# FIXME: Use CheckLinkerFlag module once cmake minimum version >= 3.18
include(CMakePushCheckState)
include(CheckCSourceCompiles)
cmake_push_check_state(RESET)
set(CMAKE_REQUIRED_LINK_OPTIONS "${flag}")
check_c_source_compiles("int main() { return 0; }" ${var})
cmake_pop_check_state()
endmacro()
function(sdl_target_link_options_no_undefined TARGET)
if(NOT MSVC AND NOT CMAKE_SYSTEM_NAME MATCHES ".*OpenBSD.*")
if(CMAKE_C_COMPILER_ID MATCHES "AppleClang")
target_link_options(${TARGET} PRIVATE "-Wl,-undefined,error")
else()
sdl_check_linker_flag("-Wl,--no-undefined" HAVE_WL_NO_UNDEFINED)
if(HAVE_WL_NO_UNDEFINED AND NOT ((CMAKE_C_COMPILER_ID MATCHES "Clang") AND WIN32))
target_link_options(${TARGET} PRIVATE "-Wl,--no-undefined")
endif()
endif()
endif()
endfunction()

View File

@@ -0,0 +1,49 @@
# This cmake build script is meant for verifying the various CMake configuration script.
cmake_minimum_required(VERSION 3.12)
project(sdl_test LANGUAGES C)
cmake_policy(SET CMP0074 NEW)
# Override CMAKE_FIND_ROOT_PATH_MODE to allow search for SDL2_ttf outside of sysroot
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE NEVER)
include(FeatureSummary)
option(TEST_SHARED "Test linking to shared SDL2_ttf library" ON)
add_feature_info("TEST_SHARED" TEST_SHARED "Test linking with shared library")
option(TEST_STATIC "Test linking to static SDL2_ttf libary" ON)
add_feature_info("TEST_STATIC" TEST_STATIC "Test linking with static library")
if(TEST_SHARED)
# FIXME: in the distant future, must become REQUIRED
find_package(SDL2 CONFIG COMPONENTS SDL2)
# FIXME: and the following should be removed
if(NOT TARGET SDL2::SDL2)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/..")
include(PrivateSdlFunctions)
sdl_find_sdl2(SDL2::SDL2 2.0)
endif()
find_package(SDL2_ttf REQUIRED CONFIG)
add_executable(main_shared main.c)
target_link_libraries(main_shared PRIVATE SDL2::SDL2 SDL2_ttf::SDL2_ttf)
endif()
if(TEST_STATIC)
# FIXME: in the distant future, must become REQUIRED
find_package(SDL2 CONFIG COMPONENTS SDL2-static)
# FIXME: and the following should be removed
if(NOT TARGET SDL2::SDL2-static)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/..")
include(PrivateSdlFunctions)
sdl_find_sdl2(SDL2::SDL2-static 2.0)
endif()
# some static vendored libraries use c++ (enable CXX after `find_package` might show a warning)
enable_language(CXX)
find_package(SDL2_ttf REQUIRED CONFIG)
add_executable(main_static main.c)
target_link_libraries(main_static PRIVATE SDL2::SDL2-static SDL2_ttf::SDL2_ttf-static)
endif()
feature_summary(WHAT ALL)

View File

@@ -0,0 +1,18 @@
#define SDL_MAIN_HANDLED
#include "SDL.h"
#include "SDL_ttf.h"
#include <stdio.h>
int main(int argc, char *argv[]) {
SDL_SetMainReady();
if (SDL_Init(0) < 0) {
fprintf(stderr, "could not initialize sdl2: %s\n", SDL_GetError());
return 1;
}
if (TTF_Init() == -1) {
fprintf(stderr, "TTF_Init: %s\n", TTF_GetError());
}
TTF_Quit();
SDL_Quit();
return 0;
}