Enable function-level linking in the CMake build (smaller downstream footprint)#1485
Closed
bmehta001 wants to merge 1 commit into
Closed
Enable function-level linking in the CMake build (smaller downstream footprint)#1485bmehta001 wants to merge 1 commit into
bmehta001 wants to merge 1 commit into
Conversation
…d-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>
Contributor
There was a problem hiding this comment.
Pull request overview
Enables function-/data-level sectioning in the CMake build so downstream consumers can dead-strip unused SDK code (especially in the vcpkg/MSVC configuration), reducing linked binary footprint without changing source or ABI.
Changes:
- Adds a new global “dead-strip enablement” block that applies in both vendored and vcpkg modes.
- Enables
/Gy /Gwfor MSVC and-ffunction-sections/-fdata-sectionsfor GCC/Clang (with an AppleClang-specific choice).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+209
to
+214
| # --- 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 |
Comment on lines
+220
to
+223
| 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) |
Contributor
Author
|
Folding this into the existing vcpkg PR #1475 instead of a standalone PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The compiler-flags block in
CMakeLists.txt(lines ~139–207) that sets-ffunction-sections/-fdata-sections/--gc-sectionsis wrapped entirely inif(NOT MATSDK_USE_VCPKG_DEPS), and its MSVC branch only sets warning flags — never/Gy.So the CMake/vcpkg build — the one packaged for downstream consumers (e.g. Foundry Local) — compiles every TU without function-level COMDATs. Referencing a single symbol then drags the whole
.objinto the consumer's image, because the linker can only include/exclude entire object files, not individual functions. Measured impact downstream: linking 1DS adds ~3.86 MB to a consumer DLL even when only a slice of the API is used.The MSBuild Release projects already do the right thing:
The CMake path just never got the equivalent.
Change
Add a small block, applied in both vendored and vcpkg modes, that splits functions/data into separate COMDATs/sections:
/Gy /Gw-ffunction-sections -fdata-sections-ffunction-sections(mach-o dead-strips per symbol)This lets a consumer's linker dead-strip unreferenced SDK code (
/OPT:REF+/OPT:ICF,--gc-sections,-dead_strip). No source or ABI change — purely a code-generation/linkage hint.Validation
/Gy /Gwreach the SDK target's compile line underMATSDK_USE_VCPKG_DEPS=ONwith the MSVC + Ninja generator.Consumer note (not in this repo)
MSVC defaults
/OPT:REFand/OPT:ICFoff when/DEBUGis set (e.g.RelWithDebInfo), so the consuming link must enable them explicitly to realize the savings. This PR only enables the compile-side prerequisite.A follow-up could also add
/OPT:REF,ICF/--gc-sectionsto the SDK's own shared-library link for dynamic vcpkg consumers.