Skip to content

Commit 542ab19

Browse files
committed
CMake: Handle GODOT_DEV_BUILD flag correctly
.dev is added to output artifacts Warn users for specifying dev_build and Release build config Update documentation with deviations to SCons Update debug_symbols handling, its rolled into build config Cleanup helper variables and comments
1 parent 163189f commit 542ab19

File tree

6 files changed

+85
-94
lines changed

6 files changed

+85
-94
lines changed

CMakeLists.txt

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ CMAKE_PROJECT_<PROJECT-NAME>_INCLUDE which was introduced in version 3.17
1212
Scons Compatibility
1313
-------------------
1414
15+
There is an understandable conflict between build systems as they define
16+
similar concepts in different ways. When there isn't a 1:1 relationship,
17+
compromises need to be made to resolve those differences.
18+
1519
As we are attempting to maintain feature parity, and ease of maintenance, these
16-
CMake scripts are built to resemble the SCons build system.
20+
CMake scripts are built to resemble the SCons build system wherever possible.
1721
1822
The file structure and file content are made to match, if not in content then
1923
in spirit. The closer the two build systems look the easier they will be to
@@ -30,58 +34,12 @@ function is run.
3034
cpp_tool = Tool("godotcpp", toolpath=["tools"])
3135
cpp_tool.options(opts, env)
3236
33-
3437
The CMake equivalent is below.
3538
]=======================================================================]
3639

3740
include( cmake/godotcpp.cmake )
3841
godotcpp_options()
3942

40-
#[=======================================================================[.rst:
41-
42-
Configurations
43-
--------------
44-
45-
There are two build main configurations, 'Debug' and 'Release', these are not
46-
related to godot's DEBUG_FEATURES flag. Build configurations change the default
47-
compiler and linker flags present when building the library, things like debug
48-
symbols, optimization.
49-
50-
The Scons build scripts don't have this concept, you can think of it like the
51-
SCons solution has a single default configuration. In both cases overriding the
52-
defaults is controlled by options on the command line, or in preset files.
53-
54-
Because of this added configuration and that it can be undefined, it becomes
55-
important to set a default, considering the SCons solution that does not enable
56-
debug symbols by default, it seemed appropriate to set the default to 'Release'
57-
if unspecified. This can always be overridden like below.
58-
59-
.. highlight:: shell
60-
61-
cmake <source> -DCMAKE_BUILD_TYPE:STRING=Debug
62-
63-
.. caution::
64-
65-
A complication arises from `Multi-Config Generators`_ that cannot have
66-
their configuration set at configure time. This means that the configuration
67-
must be set on the build command. This is especially important for Visual
68-
Studio Generators which default to 'Debug'
69-
70-
.. highlight:: shell
71-
72-
cmake --build . --config Release
73-
74-
.. _Multi-Config Generators:https://cmake.org/cmake/help/latest/prop_gbl/GENERATOR_IS_MULTI_CONFIG.html
75-
]=======================================================================]
76-
get_property( IS_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG )
77-
if( NOT IS_MULTI_CONFIG AND NOT CMAKE_BUILD_TYPE )
78-
if( GODOT_DEV_BUILD )
79-
set( CMAKE_BUILD_TYPE Debug )
80-
else ()
81-
set( CMAKE_BUILD_TYPE Release )
82-
endif ()
83-
endif ()
84-
8543
#[[ Python is required for code generation ]]
8644
find_package(Python3 3.4 REQUIRED) # pathlib should be present
8745

cmake/common_compiler_flags.cmake

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ flags like optimization levels, warnings, and features. For platform specific
77
flags look to each of the ``cmake/<platform>.cmake`` files.
88
99
]=======================================================================]
10-
#Generator Expression Helpers
10+
11+
#[[ Compiler Configuration, not to be confused with build targets ]]
12+
set( DEBUG_SYMBOLS "$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>" )
13+
14+
#[[ Compiler Identification ]]
1115
set( IS_CLANG "$<CXX_COMPILER_ID:Clang>" )
1216
set( IS_APPLECLANG "$<CXX_COMPILER_ID:AppleClang>" )
1317
set( IS_GNU "$<CXX_COMPILER_ID:GNU>" )
@@ -20,16 +24,7 @@ set( GNU_GT_V11 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,11>" )
2024
set( GNU_LT_V11 "$<VERSION_LESS:$<CXX_COMPILER_VERSION>,11>" )
2125
set( GNU_GE_V12 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,12>" )
2226

23-
set( HOT_RELOAD-UNSET "$<STREQUAL:${GODOT_USE_HOT_RELOAD},>")
24-
25-
set( DISABLE_EXCEPTIONS "$<BOOL:${GODOT_DISABLE_EXCEPTIONS}>")
26-
27-
2827
function( common_compiler_flags TARGET_NAME )
29-
set( IS_RELEASE "$<STREQUAL:${TARGET_NAME},template_release>")
30-
set( DEBUG_FEATURES "$<OR:$<STREQUAL:${TARGET_NAME},template_debug>,$<STREQUAL:${TARGET_NAME},editor>>" )
31-
set( HOT_RELOAD "$<IF:${HOT_RELOAD-UNSET},$<NOT:${IS_RELEASE}>,$<BOOL:${GODOT_USE_HOT_RELOAD}>>" )
32-
set( DEBUG_SYMBOLS "$<BOOL:${GODOT_DEBUG_SYMBOLS}>" )
3328

3429
target_compile_features(${TARGET_NAME}
3530
PUBLIC
@@ -44,24 +39,19 @@ function( common_compiler_flags TARGET_NAME )
4439
$<${DISABLE_EXCEPTIONS}:
4540
$<${NOT_MSVC}:-fno-exceptions>
4641
>
47-
$<$<NOT:${DISABLE_EXCEPTIONS}>:
48-
$<${IS_MSVC}:/EHsc>
49-
>
5042

5143
# Enabling Debug Symbols
5244
$<${DEBUG_SYMBOLS}:
53-
$<${IS_MSVC}: /Zi /FS>
54-
5545
# Adding dwarf-4 explicitly makes stacktraces work with clang builds,
5646
# otherwise addr2line doesn't understand them.
5747
$<${NOT_MSVC}:
5848
-gdwarf-4
59-
$<IF:${IS_DEV},-g3,-g2>
49+
$<IF:${IS_DEV_BUILD},-g3,-g2>
6050
>
6151
>
6252

63-
$<${IS_DEV}:
64-
$<${NOT_MSVC}:-fno-omit-frame-pointer -O0 -g>
53+
$<${IS_DEV_BUILD}:
54+
$<${NOT_MSVC}:-fno-omit-frame-pointer -O0>
6555
>
6656

6757
$<${HOT_RELOAD}:
@@ -137,6 +127,8 @@ function( common_compiler_flags TARGET_NAME )
137127
# features
138128
$<${DEBUG_FEATURES}:DEBUG_ENABLED DEBUG_METHODS_ENABLED>
139129

130+
$<${IS_DEV_BUILD}:DEV_ENABLED>
131+
140132
$<${HOT_RELOAD}:HOT_RELOAD_ENABLED>
141133

142134
$<$<STREQUAL:${GODOT_PRECISION},double>:REAL_T_IS_DOUBLE>

cmake/godotcpp.cmake

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22
godotcpp.cmake
33
--------------
44
5+
As godot-cpp is a C++ project, there are no C files, and detection of a C
6+
compiler is unnecessary. When CMake performs the configure process, if a
7+
C compiler is specified, like in a toolchain, or from an IDE, then it will
8+
print a warning stating that the CMAKE_C_COMPILER compiler is unused.
9+
This if statement simply silences that warning.
10+
]=======================================================================]
11+
if( CMAKE_C_COMPILER )
12+
endif ()
13+
14+
#[=======================================================================[.rst:
15+
Include Platform Files
16+
----------------------
17+
518
Because these files are included into the top level CMakelists.txt before the
619
project directive, it means that
720
@@ -18,10 +31,7 @@ include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/macos.cmake)
1831
include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/web.cmake)
1932
include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/windows.cmake)
2033

21-
#Silence warning from unused CMAKE_C_COMPILER from toolchain
22-
if( CMAKE_C_COMPILER )
23-
endif ()
24-
34+
# Detect number of processors
2535
include(ProcessorCount)
2636
ProcessorCount(PROC_MAX)
2737
message( "Auto-detected ${PROC_MAX} CPU cores available for build parallelism." )
@@ -99,8 +109,7 @@ function( godotcpp_options )
99109
#TODO threads
100110
#TODO compiledb
101111
#TODO compiledb_file
102-
103-
#NOTE: build_profile's equivalent in cmake is CMakePresets.json
112+
#TODO build_profile
104113

105114
set(GODOT_USE_HOT_RELOAD "" CACHE BOOL
106115
"Enable the extra accounting required to support hot reload. (ON|OFF)")
@@ -114,17 +123,26 @@ function( godotcpp_options )
114123
set_property( CACHE GODOT_SYMBOL_VISIBILITY PROPERTY STRINGS "auto;visible;hidden" )
115124

116125
#TODO optimize
117-
#TODO debug_symbols
118-
option( GODOT_DEBUG_SYMBOLS "" OFF )
126+
119127
option( GODOT_DEV_BUILD "Developer build with dev-only debugging code (DEV_ENABLED)" OFF )
120128

129+
#[[ debug_symbols
130+
Debug symbols are enabled by using the Debug or RelWithDebInfo build configurations.
131+
Single Config Generator is set at configure time
132+
133+
cmake ../ -DCMAKE_BUILD_TYPE=Debug
134+
135+
Multi-Config Generator is set at build time
136+
137+
cmake --build . --config Debug
138+
139+
]]
140+
121141
# FIXME These options are not present in SCons, and perhaps should be added there.
122142
option( GODOT_SYSTEM_HEADERS "Expose headers as SYSTEM." OFF )
123143
option( GODOT_WARNING_AS_ERROR "Treat warnings as errors" OFF )
124144

125-
# Run options commands on the following to populate cache for all
126-
# platforms. This type of thing is typically done conditionally But as
127-
# scons shows all options so shall we.
145+
#[[ Target Platform Options ]]
128146
android_options()
129147
ios_options()
130148
linux_options()
@@ -136,7 +154,6 @@ endfunction()
136154
# Function to configure and generate the targets
137155
function( godotcpp_generate )
138156
#[[ Multi-Threaded MSVC Compilation
139-
140157
When using the MSVC compiler the build command -j <n> only specifies
141158
parallel jobs or targets, and not multi-threaded compilation To speed up
142159
compile times on msvc, the /MP <n> flag can be set. But we need to set it
@@ -157,13 +174,11 @@ function( godotcpp_generate )
157174

158175
#[[ GODOT_SYMBOL_VISIBLITY
159176
To match the SCons options, the allowed values are "auto", "visible", and "hidden"
160-
This effects the compiler flag -fvisibility=[default|internal|hidden|protected]
161-
The corresponding CMake option CXX_VISIBILITY_PRESET accepts the compiler values.
177+
This effects the compiler flag_ -fvisibility=[default|internal|hidden|protected]
178+
The corresponding target option CXX_VISIBILITY_PRESET accepts the compiler values.
162179
163180
TODO: It is probably worth a pull request which changes both to use the compiler values
164-
https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#index-fvisibility
165-
166-
This performs the necessary conversion
181+
.. _flag:https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#index-fvisibility
167182
]]
168183
if( ${GODOT_SYMBOL_VISIBILITY} STREQUAL "auto" OR ${GODOT_SYMBOL_VISIBILITY} STREQUAL "visible" )
169184
set( GODOT_SYMBOL_VISIBILITY "default" )
@@ -230,14 +245,29 @@ function( godotcpp_generate )
230245
godot_arch_map( SYSTEM_ARCH ${CMAKE_SYSTEM_PROCESSOR} )
231246
endif()
232247

248+
# Transform options into generator expressions
249+
set( HOT_RELOAD-UNSET "$<STREQUAL:${GODOT_USE_HOT_RELOAD},>")
250+
251+
set( DISABLE_EXCEPTIONS "$<BOOL:${GODOT_DISABLE_EXCEPTIONS}>")
252+
253+
# GODOT_DEV_BUILD
254+
set( RELEASE_TYPES "Release;MinSizeRel")
255+
get_property( IS_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG )
256+
if( IS_MULTI_CONFIG )
257+
message( NOTICE "=> Default build type is Debug. For other build types add --config <type> to build command")
258+
elseif( GODOT_DEV_BUILD AND CMAKE_BUILD_TYPE IN_LIST RELEASE_TYPES )
259+
message( WARNING "=> GODOT_DEV_BUILD implies a Debug-like build but CMAKE_BUILD_TYPE is '${CMAKE_BUILD_TYPE}'")
260+
endif ()
261+
set( IS_DEV_BUILD "$<BOOL:${GODOT_DEV_BUILD}>")
262+
# The .dev portion of the name if GODOT_DEV_BUILD is true.
263+
set( DEV_TAG "$<${IS_DEV_BUILD}:.dev>" )
264+
233265
### Define our godot-cpp library targets
234266
foreach ( TARGET_NAME template_debug template_release editor )
235267

236-
# Useful genex snippits used in subsequent genex's
237-
set( IS_RELEASE "$<STREQUAL:${TARGET_NAME},template_release>")
238-
set( IS_DEV "$<BOOL:${GODOT_DEV_BUILD}>")
239-
set( DEBUG_FEATURES "$<OR:$<STREQUAL:${TARGET_NAME},template_debug>,$<STREQUAL:${TARGET_NAME},editor>>" )
240-
set( HOT_RELOAD "$<IF:${HOT_RELOAD-UNSET},$<NOT:${IS_RELEASE}>,$<BOOL:${GODOT_USE_HOT_RELOAD}>>" )
268+
# Generator Expressions that rely on the target
269+
set( DEBUG_FEATURES "$<NOT:$<STREQUAL:${TARGET_NAME},template_release>>" )
270+
set( HOT_RELOAD "$<IF:${HOT_RELOAD-UNSET},${DEBUG_FEATURES},$<BOOL:${GODOT_USE_HOT_RELOAD}>>" )
241271

242272
# the godot-cpp.* library targets
243273
add_library( ${TARGET_NAME} STATIC EXCLUDE_FROM_ALL )
@@ -268,7 +298,7 @@ function( godotcpp_generate )
268298
BUILD_RPATH_USE_ORIGIN ON
269299

270300
PREFIX lib
271-
OUTPUT_NAME "${PROJECT_NAME}.${SYSTEM_NAME}.${TARGET_NAME}.${SYSTEM_ARCH}"
301+
OUTPUT_NAME "${PROJECT_NAME}.${SYSTEM_NAME}.${TARGET_NAME}${DEV_TAG}.${SYSTEM_ARCH}"
272302
ARCHIVE_OUTPUT_DIRECTORY "$<1:${CMAKE_BINARY_DIR}/bin>"
273303

274304
# Things that are handy to know for dependent targets

cmake/windows.cmake

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,7 @@ endfunction()
5959

6060
#[===========================[ Target Generation ]===========================]
6161
function( windows_generate TARGET_NAME )
62-
set( IS_MSVC "$<CXX_COMPILER_ID:MSVC>" )
63-
set( IS_CLANG "$<OR:$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:Clang>>" )
64-
set( NOT_MSVC "$<NOT:${IS_MSVC}>" )
6562
set( STATIC_CPP "$<BOOL:${GODOT_USE_STATIC_CPP}>")
66-
set( DISABLE_EXCEPTIONS "$<BOOL:${GODOT_DISABLE_EXCEPTIONS}>")
6763
set( DEBUG_CRT "$<BOOL:${GODOT_DEBUG_CRT}>" )
6864

6965
set_target_properties( ${TARGET_NAME}

doc/cmake.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ Configuration examples are listed at the bottom of the page.
2424

2525
.. _godot-cpp-template: https://github.com/godotengine/godot-cpp-template
2626

27+
SCons Deviations
28+
----------------
29+
30+
Not everything from SCons can be perfectly representable in CMake, here are
31+
the notable differences.
32+
33+
- debug_symbols
34+
No longer has an explicit option, and is enabled via Debug-like CMake
35+
build configurations; Debug, RelWithDebInfo.
36+
37+
- dev_build
38+
Does not define NDEBUG when disabled, NDEBUG is set via Release-like
39+
CMake build configurations; Release, MinSizeRel.
40+
2741
Basic walkthrough
2842
-----------------
2943

test/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ get_target_property( GODOT_TARGET godot-cpp::${TEST_TARGET} GODOT_TARGET )
2626
get_target_property( GODOT_ARCH godot-cpp::${TEST_TARGET} GODOT_ARCH )
2727

2828
set( OUTPUT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/project/bin/" )
29+
set( DEV_TAG "$<$<BOOL:${GODOT_DEV_BUILD}>:.dev>" )
2930

3031
set_target_properties( godot-cpp-test
3132
PROPERTIES
@@ -45,7 +46,7 @@ set_target_properties( godot-cpp-test
4546
PDB_OUTPUT_DIRECTORY "$<1:${OUTPUT_DIR}>" #MSVC Only, ignored on other platforms
4647

4748
PREFIX "lib"
48-
OUTPUT_NAME "gdexample.${GODOT_PLATFORM}.${GODOT_TARGET}.${GODOT_ARCH}"
49+
OUTPUT_NAME "gdexample.${GODOT_PLATFORM}.${GODOT_TARGET}${DEV_TAG}.${GODOT_ARCH}"
4950
)
5051

5152
if( CMAKE_SYSTEM_NAME STREQUAL Darwin )
@@ -58,7 +59,7 @@ if( CMAKE_SYSTEM_NAME STREQUAL Darwin )
5859
LIBRARY_OUTPUT_DIRECTORY "$<1:${OUTPUT_DIR}>"
5960
RUNTIME_OUTPUT_DIRECTORY "$<1:${OUTPUT_DIR}>"
6061

61-
OUTPUT_NAME "gdexample.macos.${TEST_TARGET}"
62+
OUTPUT_NAME "gdexample.macos.${TEST_TARGET}${DEV_TAG}"
6263
SUFFIX ""
6364

6465
#macos options

0 commit comments

Comments
 (0)