From 1ab9e06e4cd584c85e679af2a2ead334160c690c Mon Sep 17 00:00:00 2001 From: David Snopek Date: Tue, 6 Oct 2026 17:46:43 -0500 Subject: [PATCH] Reduce binary size by removing unused functions (even when not using LTO) --- cmake/android.cmake | 2 ++ cmake/common_compiler_flags.cmake | 4 +++- cmake/ios.cmake | 2 ++ cmake/linux.cmake | 1 + cmake/macos.cmake | 2 ++ cmake/windows.cmake | 1 + tools/android.py | 2 ++ tools/common_compiler_flags.py | 5 ++++- tools/ios.py | 2 ++ tools/linux.py | 2 ++ tools/macos.py | 2 ++ tools/windows.py | 4 ++-- 12 files changed, 25 insertions(+), 4 deletions(-) diff --git a/cmake/android.cmake b/cmake/android.cmake index 070c6eb48..a8b3043bd 100644 --- a/cmake/android.cmake +++ b/cmake/android.cmake @@ -45,5 +45,7 @@ endfunction() function(android_generate) target_compile_definitions(godot-cpp PUBLIC ANDROID_ENABLED UNIX_ENABLED) + target_link_options(godot-cpp PUBLIC -Wl,--gc-sections) + common_compiler_flags() endfunction() diff --git a/cmake/common_compiler_flags.cmake b/cmake/common_compiler_flags.cmake index e95dd491b..3e8589c42 100644 --- a/cmake/common_compiler_flags.cmake +++ b/cmake/common_compiler_flags.cmake @@ -81,6 +81,8 @@ function(common_compiler_flags) $<${HOT_RELOAD}:$<${IS_GNU}:-fno-gnu-unique>> + $<${NOT_MSVC}:$<$>:-ffunction-sections -fdata-sections>> + # MSVC only $<${IS_MSVC}: # /MP isn't valid for clang-cl with msvc frontend @@ -185,7 +187,7 @@ function(common_compiler_flags) $<$: $<${IS_GNU}:-s> $<${IS_CLANG}:-s> - $<${IS_APPLECLANG}:-Wl,-S -Wl,-x -Wl,-dead_strip> + $<${IS_APPLECLANG}:-Wl,-S -Wl,-x> > PRIVATE $<${IS_MSVC}: diff --git a/cmake/ios.cmake b/cmake/ios.cmake index 52b2c012a..ac763e909 100644 --- a/cmake/ios.cmake +++ b/cmake/ios.cmake @@ -32,5 +32,7 @@ endfunction() function(ios_generate) target_compile_definitions(godot-cpp PUBLIC IOS_ENABLED UNIX_ENABLED) + target_link_options(godot-cpp PUBLIC -Wl,-dead_strip) + common_compiler_flags() endfunction() diff --git a/cmake/linux.cmake b/cmake/linux.cmake index c782bce3f..d6fbd1560 100644 --- a/cmake/linux.cmake +++ b/cmake/linux.cmake @@ -28,6 +28,7 @@ function(linux_generate) target_link_options( godot-cpp PUBLIC + -Wl,--gc-sections $<${STATIC_CPP}: -static-libgcc -static-libstdc++ diff --git a/cmake/macos.cmake b/cmake/macos.cmake index 33db862d7..3a6d986cf 100644 --- a/cmake/macos.cmake +++ b/cmake/macos.cmake @@ -37,5 +37,7 @@ endfunction() function(macos_generate) target_compile_definitions(godot-cpp PUBLIC MACOS_ENABLED UNIX_ENABLED) + target_link_options(godot-cpp PUBLIC -Wl,-dead_strip) + common_compiler_flags() endfunction() diff --git a/cmake/windows.cmake b/cmake/windows.cmake index 7478a5a01..4286952ca 100644 --- a/cmake/windows.cmake +++ b/cmake/windows.cmake @@ -103,6 +103,7 @@ function(windows_generate) PUBLIC $<${NOT_MSVC}: -Wl,--no-undefined + -Wl,--gc-sections $<${STATIC_CPP}: -static -static-libgcc diff --git a/tools/android.py b/tools/android.py index e63c1fd27..19fc7e963 100644 --- a/tools/android.py +++ b/tools/android.py @@ -118,6 +118,8 @@ def generate(env): env.Append(CCFLAGS=arch_info["ccflags"]) env.Append(LINKFLAGS=["--target=" + arch_info["target"] + env["android_api_level"], "-march=" + arch_info["march"]]) + env.Append(LINKFLAGS=["-Wl,--gc-sections"]) + env.Append(CPPDEFINES=["ANDROID_ENABLED", "UNIX_ENABLED"]) # Refer to https://github.com/godotengine/godot/blob/master/platform/android/detect.py diff --git a/tools/common_compiler_flags.py b/tools/common_compiler_flags.py index d5013e828..9a0a60a6a 100644 --- a/tools/common_compiler_flags.py +++ b/tools/common_compiler_flags.py @@ -49,6 +49,9 @@ def generate(env): env.Append(CXXFLAGS=["/EHsc"]) if not env.get("is_msvc", False): + if not env["use_mingw"]: + env.Append(CCFLAGS=["-ffunction-sections", "-fdata-sections"]) + if env["symbols_visibility"] == "visible": env.Append(CCFLAGS=["-fvisibility=default"]) env.Append(LINKFLAGS=["-fvisibility=default"]) @@ -108,7 +111,7 @@ def generate(env): else: if using_clang(env) and not is_vanilla_clang(env) and not env["use_mingw"]: # Apple Clang, its linker doesn't like -s. - env.Append(LINKFLAGS=["-Wl,-S", "-Wl,-x", "-Wl,-dead_strip"]) + env.Append(LINKFLAGS=["-Wl,-S", "-Wl,-x"]) else: env.Append(LINKFLAGS=["-s"]) diff --git a/tools/ios.py b/tools/ios.py index 4e2a45c22..85e060650 100644 --- a/tools/ios.py +++ b/tools/ios.py @@ -97,6 +97,8 @@ def generate(env): env.Append(CCFLAGS=["-isysroot", env["IOS_SDK_PATH"]]) env.Append(LINKFLAGS=["-isysroot", env["IOS_SDK_PATH"], "-F" + env["IOS_SDK_PATH"]]) + env.Append(LINKFLAGS=["-Wl,-dead_strip"]) + env.Append(CPPDEFINES=["IOS_ENABLED", "UNIX_ENABLED"]) # Refer to https://github.com/godotengine/godot/blob/master/platform/ios/detect.py: diff --git a/tools/linux.py b/tools/linux.py index 2975c68a6..cc10ff2fb 100644 --- a/tools/linux.py +++ b/tools/linux.py @@ -42,6 +42,8 @@ def generate(env): env.Append(CCFLAGS=["-march=rv64gc"]) env.Append(LINKFLAGS=["-march=rv64gc"]) + env.Append(LINKFLAGS=["-Wl,--gc-sections"]) + # Link statically for portability if env["use_static_cpp"]: env.Append(LINKFLAGS=["-static-libgcc", "-static-libstdc++"]) diff --git a/tools/macos.py b/tools/macos.py index 541249cb4..c306113d5 100644 --- a/tools/macos.py +++ b/tools/macos.py @@ -63,6 +63,8 @@ def generate(env): env.Append(CCFLAGS=["-isysroot", env["macos_sdk_path"]]) env.Append(LINKFLAGS=["-isysroot", env["macos_sdk_path"]]) + env.Append(LINKFLAGS=["-Wl,-dead_strip"]) + env.Append(CPPDEFINES=["MACOS_ENABLED", "UNIX_ENABLED"]) # Refer to https://github.com/godotengine/godot/blob/master/platform/macos/detect.py diff --git a/tools/windows.py b/tools/windows.py index 9aad0d80a..c0ba6cfc8 100644 --- a/tools/windows.py +++ b/tools/windows.py @@ -140,7 +140,7 @@ def generate(env): env["SHLIBSUFFIX"] = ".dll" env.Append(CCFLAGS=["-Wwrite-strings"]) - env.Append(LINKFLAGS=["-Wl,--no-undefined"]) + env.Append(LINKFLAGS=["-Wl,--no-undefined", "-Wl,--gc-sections"]) if env["use_static_cpp"]: env.Append( LINKFLAGS=[ @@ -186,7 +186,7 @@ def generate(env): env["SHLIBSUFFIX"] = ".dll" env.Append(CCFLAGS=["-Wwrite-strings"]) - env.Append(LINKFLAGS=["-Wl,--no-undefined"]) + env.Append(LINKFLAGS=["-Wl,--no-undefined", "-Wl,--gc-sections"]) if env["use_static_cpp"]: env.Append( LINKFLAGS=[