From 268c44b12edaeff3f66bbe35374c47fcf4ffaf85 Mon Sep 17 00:00:00 2001 From: Bhagirath Mehta Date: Mon, 15 Jun 2026 12:21:44 -0500 Subject: [PATCH] Enable function-level linking in the CMake build so consumers can dead-strip The compiler-flags block in CMakeLists.txt (the one that sets -ffunction-sections/-fdata-sections/--gc-sections) is wrapped entirely in `if(NOT MATSDK_USE_VCPKG_DEPS)`, and its MSVC branch only sets warning flags -- never /Gy. As a result the CMake/vcpkg build (the one packaged for downstream consumers such as Foundry Local) compiles every translation unit without function-level COMDATs. Referencing a single symbol then pulls the entire .obj into the consumer's image, because the linker can only include/exclude whole object files, not individual functions. Add a small block, applied in BOTH vendored and vcpkg modes, that splits functions/data into separate COMDATs/sections: * MSVC: /Gy /Gw * GCC / Clang: -ffunction-sections -fdata-sections * AppleClang: -ffunction-sections (mach-o dead-strips per symbol) This lets a consumer's linker dead-strip unreferenced SDK code (MSVC /OPT:REF + /OPT:ICF, GNU/Clang --gc-sections, Apple ld -dead_strip) and brings the CMake build in line with the MSBuild Release projects, which already enable FunctionLevelLinking + OptimizeReferences + EnableCOMDATFolding (Solutions/win32-lib/win32-lib.vcxproj, Solutions/win32-dll/win32-dll.vcxproj). No source or ABI change; purely a code-generation/linkage hint. Verified that /Gy /Gw reach the SDK target's compile line under MATSDK_USE_VCPKG_DEPS=ON with the MSVC + Ninja generator. Note for consumers: MSVC defaults /OPT:REF and /OPT:ICF OFF when /DEBUG is set (e.g. RelWithDebInfo), so the consuming link must enable them explicitly to realize the savings. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- CMakeLists.txt | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) 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)