Skip to content

Commit

Permalink
CMake: Enable using clang-cl on windows
Browse files Browse the repository at this point in the history
When downloading clang from llvm.org the compiler defaults to clang-cl with the msvc compiler interface.

This commit detects that scenario and sets the helper variables to account for it.

Cleans up the other duplicated helper variables that are still in scope.
And fixes formatting.
  • Loading branch information
enetheru committed Dec 4, 2024
1 parent bb56706 commit 5fcb2da
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 93 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ project( godot-cpp
HOMEPAGE_URL "https://github.com/godotengine/godot-cpp"
LANGUAGES CXX)

helper_variables()

godotcpp_generate()

# Test Example
Expand Down
177 changes: 91 additions & 86 deletions cmake/common_compiler_flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,39 @@
Common Compiler Flags
---------------------
This file contains a single function to configure platform agnostic compiler
flags like optimization levels, warnings, and features. For platform specific
flags look to each of the ``cmake/<platform>.cmake`` files.
This file contains host platform toolchain and target platform agnostic
configuration. It includes flags like optimization levels, warnings, and
features. For target platform specific flags look to each of the
``cmake/<platform>.cmake`` files.
]=======================================================================]
#Generator Expression Helpers
set( IS_CLANG "$<CXX_COMPILER_ID:Clang>" )
set( IS_APPLECLANG "$<CXX_COMPILER_ID:AppleClang>" )
set( IS_GNU "$<CXX_COMPILER_ID:GNU>" )
set( IS_MSVC "$<CXX_COMPILER_ID:MSVC>" )
set( NOT_MSVC "$<NOT:$<CXX_COMPILER_ID:MSVC>>" )

set( GNU_LT_V8 "$<VERSION_LESS:$<CXX_COMPILER_VERSION>,8>" )
set( GNU_GE_V9 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,9>" )
set( GNU_GT_V11 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,11>" )
set( GNU_LT_V11 "$<VERSION_LESS:$<CXX_COMPILER_VERSION>,11>" )
set( GNU_GE_V12 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,12>" )

set( HOT_RELOAD-UNSET "$<STREQUAL:${GODOT_USE_HOT_RELOAD},>")

set( DISABLE_EXCEPTIONS "$<BOOL:${GODOT_DISABLE_EXCEPTIONS}>")

#Generator Expression Helpers
function(helper_variables)
set( IS_CLANG "$<CXX_COMPILER_ID:Clang>" PARENT_SCOPE )
set( IS_APPLECLANG "$<CXX_COMPILER_ID:AppleClang>" PARENT_SCOPE )
set( IS_GNU "$<CXX_COMPILER_ID:GNU>" PARENT_SCOPE )
set( IS_MSVC "$<CXX_COMPILER_ID:MSVC>" PARENT_SCOPE )
set( NOT_MSVC "$<NOT:$<CXX_COMPILER_ID:MSVC>>" PARENT_SCOPE )

# LLVM/Clang can imitate the MSVC options interface.
if( ${CMAKE_CXX_COMPILER_ID} STREQUAL Clang )
if( ${CMAKE_CXX_COMPILER_FRONTEND_VARIANT} STREQUAL MSVC )
set( IS_CLANG "0" PARENT_SCOPE )
set( IS_MSVC "1" PARENT_SCOPE )
set( NOT_MSVC "0" PARENT_SCOPE )
endif ()
endif ()

#GNU Compiler version variants
set( GNU_LT_V8 "$<VERSION_LESS:$<CXX_COMPILER_VERSION>,8>" PARENT_SCOPE )
set( GNU_GE_V9 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,9>" PARENT_SCOPE )
set( GNU_GT_V11 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,11>" PARENT_SCOPE )
set( GNU_LT_V11 "$<VERSION_LESS:$<CXX_COMPILER_VERSION>,11>" PARENT_SCOPE )
set( GNU_GE_V12 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,12>" PARENT_SCOPE )
endfunction()

function( common_compiler_flags TARGET_NAME )
set( IS_RELEASE "$<STREQUAL:${TARGET_NAME},template_release>")
set( DEBUG_FEATURES "$<OR:$<STREQUAL:${TARGET_NAME},template_debug>,$<STREQUAL:${TARGET_NAME},editor>>" )
set( HOT_RELOAD "$<IF:${HOT_RELOAD-UNSET},$<NOT:${IS_RELEASE}>,$<BOOL:${GODOT_USE_HOT_RELOAD}>>" )
set( DEBUG_SYMBOLS "$<BOOL:${GODOT_DEBUG_SYMBOLS}>" )

target_compile_features(${TARGET_NAME}
PUBLIC
Expand All @@ -38,7 +43,7 @@ function( common_compiler_flags TARGET_NAME )

# These compiler options reflect what is in godot/SConstruct.
target_compile_options( ${TARGET_NAME}
PUBLIC
PUBLIC
# Disable exception handling. Godot doesn't use exceptions anywhere, and this
# saves around 20% of binary size and very significant build time.
$<${DISABLE_EXCEPTIONS}:
Expand Down Expand Up @@ -68,70 +73,70 @@ function( common_compiler_flags TARGET_NAME )
$<${IS_GNU}:-fno-gnu-unique>
>

# MSVC only
$<${IS_MSVC}:
"/MP ${PROC_N}"
/W4

# Disable warnings which we don't plan to fix.
/wd4100 # C4100 (unreferenced formal parameter): Doesn't play nice with polymorphism.
/wd4127 # C4127 (conditional expression is constant)
/wd4201 # C4201 (non-standard nameless struct/union): Only relevant for C89.
/wd4244 # C4244 C4245 C4267 (narrowing conversions): Unavoidable at this scale.
/wd4245
/wd4267
/wd4305 # C4305 (truncation): double to float or real_t, too hard to avoid.
/wd4514 # C4514 (unreferenced inline function has been removed)
/wd4714 # C4714 (function marked as __forceinline not inlined)
/wd4820 # C4820 (padding added after construct)

/utf-8
>

# Clang and GNU common options
$<$<OR:${IS_CLANG},${IS_GNU}>:
-Wall
-Wctor-dtor-privacy
-Wextra
-Wno-unused-parameter
-Wnon-virtual-dtor
-Wwrite-strings
>

# Clang only
$<${IS_CLANG}:
-Wimplicit-fallthrough
-Wno-ordered-compare-function-pointers
>

# GNU only
$<${IS_GNU}:
-Walloc-zero
-Wduplicated-branches
-Wduplicated-cond
-Wno-misleading-indentation
-Wplacement-new=1
-Wshadow-local
-Wstringop-overflow=4

# Bogus warning fixed in 8+.
$<${GNU_LT_V8}:-Wno-strict-overflow>

$<${GNU_GE_V9}:-Wattribute-alias=2>

# Broke on MethodBind templates before GCC 11.
$<${GNU_GT_V11}:-Wlogical-op>

# Regression in GCC 9/10, spams so much in our variadic templates that we need to outright disable it.
$<${GNU_LT_V11}:-Wno-type-limits>

# False positives in our error macros, see GH-58747.
$<${GNU_GE_V12}:-Wno-return-type>
>
# MSVC only
$<${IS_MSVC}:
/MP${PROC_N}
/W4

# Disable warnings which we don't plan to fix.
/wd4100 # C4100 (unreferenced formal parameter): Doesn't play nice with polymorphism.
/wd4127 # C4127 (conditional expression is constant)
/wd4201 # C4201 (non-standard nameless struct/union): Only relevant for C89.
/wd4244 # C4244 C4245 C4267 (narrowing conversions): Unavoidable at this scale.
/wd4245
/wd4267
/wd4305 # C4305 (truncation): double to float or real_t, too hard to avoid.
/wd4514 # C4514 (unreferenced inline function has been removed)
/wd4714 # C4714 (function marked as __forceinline not inlined)
/wd4820 # C4820 (padding added after construct)

/utf-8
>

# Clang and GNU common options
$<$<OR:${IS_CLANG},${IS_GNU}>:
-Wall
-Wctor-dtor-privacy
-Wextra
-Wno-unused-parameter
-Wnon-virtual-dtor
-Wwrite-strings
>

# Clang only
$<${IS_CLANG}:
-Wimplicit-fallthrough
-Wno-ordered-compare-function-pointers
>

# GNU only
$<${IS_GNU}:
-Walloc-zero
-Wduplicated-branches
-Wduplicated-cond
-Wno-misleading-indentation
-Wplacement-new=1
-Wshadow-local
-Wstringop-overflow=4

# Bogus warning fixed in 8+.
$<${GNU_LT_V8}:-Wno-strict-overflow>

$<${GNU_GE_V9}:-Wattribute-alias=2>

# Broke on MethodBind templates before GCC 11.
$<${GNU_GT_V11}:-Wlogical-op>

# Regression in GCC 9/10, spams so much in our variadic templates that we need to outright disable it.
$<${GNU_LT_V11}:-Wno-type-limits>

# False positives in our error macros, see GH-58747.
$<${GNU_GE_V12}:-Wno-return-type>
>
)

target_compile_definitions(${TARGET_NAME}
PUBLIC
PUBLIC
GDEXTENSION

# features
Expand All @@ -145,7 +150,7 @@ function( common_compiler_flags TARGET_NAME )
)

target_link_options( ${TARGET_NAME}
PUBLIC
PUBLIC
$<${IS_MSVC}:
/WX # treat link warnings as errors.
/MANIFEST:NO # We dont need a manifest
Expand Down
9 changes: 7 additions & 2 deletions cmake/godotcpp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,17 @@ function( godotcpp_generate )
godot_arch_map( SYSTEM_ARCH ${CMAKE_SYSTEM_PROCESSOR} )
endif()

set( HOT_RELOAD-UNSET "$<STREQUAL:${GODOT_USE_HOT_RELOAD},>")
set( STATIC_CPP "$<BOOL:${GODOT_USE_STATIC_CPP}>")
set( DISABLE_EXCEPTIONS "$<BOOL:${GODOT_DISABLE_EXCEPTIONS}>")
set( IS_DEV "$<BOOL:${GODOT_DEV_BUILD}>")
set( DEBUG_SYMBOLS "$<BOOL:${GODOT_DEBUG_SYMBOLS}>" )

### Define our godot-cpp library targets
foreach ( TARGET_NAME template_debug template_release editor )

# Useful genex snippits used in subsequent genex's
# These variables rely on the target name
set( IS_RELEASE "$<STREQUAL:${TARGET_NAME},template_release>")
set( IS_DEV "$<BOOL:${GODOT_DEV_BUILD}>")
set( DEBUG_FEATURES "$<OR:$<STREQUAL:${TARGET_NAME},template_debug>,$<STREQUAL:${TARGET_NAME},editor>>" )
set( HOT_RELOAD "$<IF:${HOT_RELOAD-UNSET},$<NOT:${IS_RELEASE}>,$<BOOL:${GODOT_USE_HOT_RELOAD}>>" )

Expand Down
6 changes: 1 addition & 5 deletions cmake/windows.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ function( windows_options )
endfunction()

function( windows_generate TARGET_NAME )
set( IS_MSVC "$<CXX_COMPILER_ID:MSVC>" )
set( IS_CLANG "$<OR:$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:Clang>>" )
set( NOT_MSVC "$<NOT:${IS_MSVC}>" )
set( STATIC_CPP "$<BOOL:${GODOT_USE_STATIC_CPP}>")
set( DISABLE_EXCEPTIONS "$<BOOL:${GODOT_DISABLE_EXCEPTIONS}>")

set( DEBUG_CRT "$<BOOL:${GODOT_DEBUG_CRT}>" )

set_target_properties( ${TARGET_NAME}
Expand Down

0 comments on commit 5fcb2da

Please sign in to comment.