Make CMake only include debug symbols when appropriate #1288
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.
Recently, I noticed that GDExtension shared libraries using godot-cpp that were built with CMake had significantly larger file sizes than the equivalent shared libraries built with SCons.
Pre-analysis
For the SCons project, I followed the GDExtension C++ example. To enable debug symbols, I added
debug_symbols=True
to thescons
command. When debug symbols were disabled, I also set release optimizations throughtarget=template_release
since I usedCMAKE_BUILD_TYPE=Release
with CMake to avoid setting debug symbols through the default Debug build type.For the CMake project, I used the same project but with this custom
CMakeLists.txt
file. To enable debug symbols, I configured the build withCMAKE_BUILD_TYPE=Debug
. As noted above, when not building with debug symbols, I configured the build withCMAKE_BUILD_TYPE=Release
as not setting anything would have fallen back to the default ofCMAKE_BUILD_TYPE=Debug
..so
sizeNotice how enabling debug symbols did not change the file size of the CMake-built shared library. It seems that, despite setting
CMAKE_BUILD_TYPE=Release
, debug symbols are still being added.Analysis
Looking at the
CMakeLists.txt
file in this repository, I noticed that, at least for non-MSVC compilers, the-g
flag was always being set regardless if the build was configured to be a debug build or not. This forces the project to build with debug symbols and ignores theCMAKE_BUILD_TYPE
.godot-cpp/CMakeLists.txt
Lines 101 to 109 in c1196a1
To my knowledge, there isn't an easy way for others using godot-cpp's
CMakeLists.txt
to workaround this if they wish to build their project without debug symbols, unless they strip them from the shared library after the fact.Changes
In order to allow projects to choose whether or not to build with debug symbols, I removed the line setting the
-g
flag. This is because CMake already sets the-g
flag automatically based on theCMAKE_BUILD_TYPE
.However, I also noticed several other flags, such as
-O0
and-O3
, were being set. These flags are similarly automatically set by CMake depending on the value ofCMAKE_BUILD_TYPE
. My assumption here is that the godot-cpp CMake file is trying to be as explicit as possible. So, to maintain consistency, I added the-g
flag alongside the other flags that are set when aDebug
CMake build is run.Results
.so
sizeNow, it is possible to build the shared library without debug symbols using CMake. That is about an 80% reduction in release shared library size for projects building with CMake, which also happens to be consistent with SCons.