diff --git a/CMakeLists.txt b/CMakeLists.txt index dea843b23..71b2b70d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -206,6 +206,26 @@ endif() endif() # NOT MATSDK_USE_VCPKG_DEPS (compiler flags) +# --- Dead-strip enablement (applies in BOTH vendored and vcpkg modes) --------- +# Split functions/data into separate COMDATs/sections so a consumer's linker can +# drop unreferenced SDK code: MSVC /OPT:REF + /OPT:ICF, GNU/Clang --gc-sections, +# Apple ld -dead_strip. The vendored-deps flag block above sets these only when +# NOT MATSDK_USE_VCPKG_DEPS, and never sets /Gy at all on MSVC -- so the vcpkg +# build (the one packaged for downstream consumers) and every MSVC build link +# whole .obj files instead of individual functions. This block closes that gap +# and matches the MSBuild Release projects, which already enable +# FunctionLevelLinking + OptimizeReferences + EnableCOMDATFolding. +if(MSVC) + add_compile_options(/Gy /Gw) +elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang") + # -fdata-sections historically conflicted with bitcode on AppleClang, and the + # mach-o linker already dead-strips at symbol granularity with -dead_strip. + add_compile_options(-ffunction-sections) +else() + # GCC / Clang (Linux, Android, MinGW) + add_compile_options(-ffunction-sections -fdata-sections) +endif() + include(tools/Utils.cmake) include(GNUInstallDirs) include(CMakePackageConfigHelpers)