Skip to content

cmake: make possible to build both game and engine against the engine Freetype submodule #1617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -778,16 +778,20 @@ endif()
option(PREFER_EXTERNAL_LIBS "Tries to use system libs where possible." ON)

macro(prefer_package LIB_NAME LIB_CMAKE)
if (PREFER_EXTERNAL_LIBS AND NOT NACL)
find_package(${LIB_NAME})
if (NOT ${LIB_NAME}_FOUND)
if (PREFER_EXTERNAL_LIBS AND NOT NACL)
find_package(${LIB_NAME})

if (NOT ${LIB_NAME}_FOUND)
message(WARNING "PREFER_EXTERNAL_LIBS is enabled but external ${LIB_NAME} is not found, falling back to vendored ${LIB_NAME}.")
if (NOT ${LIB_NAME}_FOUND)
message(WARNING "PREFER_EXTERNAL_LIBS is enabled but external ${LIB_NAME} is not found, falling back to vendored ${LIB_NAME}.")
endif()
endif()
endif()

if (NOT ${LIB_NAME}_FOUND)
include(${LIB_CMAKE})
if (NOT ${LIB_NAME}_FOUND)
include(${LIB_CMAKE})

set(${LIB_NAME}_FOUND ON)
endif()
endif()
endmacro()

Expand Down Expand Up @@ -817,7 +821,6 @@ if (BUILD_CLIENT)
set(LIBS_CLIENT ${LIBS_CLIENT} ${PNG_LIBRARIES})

prefer_package(Freetype ${DAEMON_DIR}/freetype.cmake)

include_directories(${FREETYPE_INCLUDE_DIRS})
set(LIBS_CLIENT ${LIBS_CLIENT} ${FREETYPE_LIBRARIES})

Expand Down
4 changes: 2 additions & 2 deletions cmake/DaemonFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ else()
# Don't set _FORTIFY_SOURCE in debug builds.
endif()

try_c_cxx_flag(FPIC "-fPIC")

if (USE_HARDENING)
# PNaCl accepts the flags but does not define __stack_chk_guard and __stack_chk_fail.
if (NOT NACL)
Expand All @@ -395,8 +397,6 @@ else()
try_c_cxx_flag(WSTACK_PROTECTOR "-Wstack-protector")

if (NOT NACL OR (NACL AND GAME_PIE))
try_c_cxx_flag(FPIC "-fPIC")

# The -pie flag requires -fPIC:
# > ld: error: relocation R_X86_64_64 cannot be used against local symbol; recompile with -fPIC
# This flag isn't used on macOS:
Expand Down
45 changes: 29 additions & 16 deletions freetype.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,37 @@ set(FREETYPE_DIR ${DAEMON_DIR}/libs/freetype)
set(FREETYPE_INCLUDE_DIRS ${FREETYPE_DIR}/include)
set(FREETYPE_LIBRARIES freetype)

option(FT_DISABLE_BROTLI "Disable Brotli" ON)
option(FT_DISABLE_BZIP2 "Disable bzip2" ON)
option(FT_DISABLE_HARFBUZZ "Disable HarfBuzz" ON)
option(FT_DISABLE_PNG "Disable PNG" ON)

if (PREFER_EXTERNAL_LIBS AND NOT NACL)
set(FREETYPE_INTERNAL_ZLIB OFF)
else()
if (NACL)
# Using Freetype's own zlib prevents the need for a zlib submodule when building the nexe cgame.
set(FREETYPE_INTERNAL_ZLIB ON)
else()
# Even if we can build an engine with Freetype using its internal zlib, we better rely on the
# external zlib even if PREFER_EXTERNAL_LIBS is OFF, because then it will avoid zlib duplication
# and share the same zlib between Freetype and the libpng.
set(FREETYPE_INTERNAL_ZLIB OFF)
endif()

if (NOT FREETYPE_INTERNAL_ZLIB)
find_package(ZLIB REQUIRED)
set(FREETYPE_LIBRARIES ${FREETYPE_LIBRARIES} ${ZLIB_LIBRARIES})
endif()

set(FT_DISABLE_ZLIB ${FREETYPE_INTERNAL_ZLIB} CACHE BOOL "Disable external zlib" FORCE)
# Do not re-add the target if already set to be built.
# For example both the engine and a native game may request Freetype
# to be built, but we need to only build once for both.
if (NOT TARGET freetype)
option(FT_DISABLE_BROTLI "Disable Brotli" ON)
option(FT_DISABLE_BZIP2 "Disable bzip2" ON)
option(FT_DISABLE_HARFBUZZ "Disable HarfBuzz" ON)
option(FT_DISABLE_PNG "Disable PNG" ON)
set(FT_DISABLE_ZLIB ${FREETYPE_INTERNAL_ZLIB} CACHE BOOL "Disable external zlib" FORCE)

add_subdirectory(${FREETYPE_DIR})
add_subdirectory(${FREETYPE_DIR})

mark_as_advanced(FT_DISABLE_BROTLI)
mark_as_advanced(FT_DISABLE_BZIP2)
mark_as_advanced(FT_DISABLE_HARFBUZZ)
mark_as_advanced(FT_DISABLE_PNG)
mark_as_advanced(FT_DISABLE_ZLIB)
mark_as_advanced(FT_ENABLE_ERROR_STRINGS)
mark_as_advanced(FT_DISABLE_BROTLI)
mark_as_advanced(FT_DISABLE_BZIP2)
mark_as_advanced(FT_DISABLE_HARFBUZZ)
mark_as_advanced(FT_DISABLE_PNG)
mark_as_advanced(FT_DISABLE_ZLIB)
mark_as_advanced(FT_ENABLE_ERROR_STRINGS)
endif()