From 74437f440a6f8990faeb70447f12fe673d46624f Mon Sep 17 00:00:00 2001 From: Thomas Debesse Date: Tue, 11 Mar 2025 15:20:31 +0100 Subject: [PATCH 1/3] cmake: make possible to build both game and engine against the engine Freetype submodule --- CMakeLists.txt | 19 +++++++++++-------- freetype.cmake | 35 ++++++++++++++++++++++------------- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8eebf887ce..cabf86fd05 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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() @@ -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}) diff --git a/freetype.cmake b/freetype.cmake index 2ba7fe93de..85c74391af 100644 --- a/freetype.cmake +++ b/freetype.cmake @@ -2,24 +2,33 @@ 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() set(FREETYPE_INTERNAL_ZLIB ON) endif() -set(FT_DISABLE_ZLIB ${FREETYPE_INTERNAL_ZLIB} CACHE BOOL "Disable external zlib" FORCE) +if (NOT FREETYPE_INTERNAL_ZLIB) + find_package(ZLIB REQUIRED) + set(FREETYPE_LIBRARIES ${FREETYPE_LIBRARIES} ${ZLIB_LIBRARIES}) +endif() + +# 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() From 3e30d43ddb724b21a6affbb8dae71db824699f82 Mon Sep 17 00:00:00 2001 From: Thomas Debesse Date: Sat, 8 Mar 2025 14:46:49 +0100 Subject: [PATCH 2/3] cmake: always use the -fPIC compiler flag Some libraries may be built statically before being linked against game dll, so -fPIC would be required, and then we better compile everything with -fPIC. --- cmake/DaemonFlags.cmake | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmake/DaemonFlags.cmake b/cmake/DaemonFlags.cmake index 416fd0ff93..036c32fbe9 100644 --- a/cmake/DaemonFlags.cmake +++ b/cmake/DaemonFlags.cmake @@ -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) @@ -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: From ea9aee272e735bedf3109e3cab85ffaec5512538 Mon Sep 17 00:00:00 2001 From: Thomas Debesse Date: Tue, 25 Mar 2025 00:00:54 +0100 Subject: [PATCH 3/3] cmake: use Freetype internal zlib only with game NaCl build --- freetype.cmake | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/freetype.cmake b/freetype.cmake index 85c74391af..f6556dbaca 100644 --- a/freetype.cmake +++ b/freetype.cmake @@ -2,10 +2,14 @@ set(FREETYPE_DIR ${DAEMON_DIR}/libs/freetype) set(FREETYPE_INCLUDE_DIRS ${FREETYPE_DIR}/include) set(FREETYPE_LIBRARIES freetype) -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)