Skip to content

Commit

Permalink
Modernise Existing CMakeLists.txt
Browse files Browse the repository at this point in the history
== godot-cpp ==
Moved find package for python to root CMakeLists.txt
Silenced warning for unused CMAKE_C_COMPILER when specified in toolchain
Changed godot-cpp compile feature cxx_std_17 to PUBLIC to propagate to consumers
Changed the MSVC warning flags to PUBLIC to propagate to consumers
Turn on WINDOWS_ENABLED based on WIN32, not IS_MSVC

New cmake/sources.cmake to collect all the pre-existing source files, because globing is evil.
Made all godot-cpp sources PRIVATE
Exposed headers as INTERFACE using target_include_directories for consumers

### Generator Expressions
Renamed generator expression helper variables to ease readability
Moved all flags to generator expressions
Moved generator expression helpers to common_compiler_flags.cmake
Refactor SYSTEM_NAME and BUILD_TYPE to use generator expressions
Refactor HOT_RELOAD to use generator expressions
CMAKE_BUILD_TYPE is no longer depended upon, eliminating config errors for multi-config builds like Visual Studio, and Ninja-MultiConfig
Now that we are specifying the config at build time, the hack to re-write CMAKE_CXX_FLAGS(_DEBUG) flags is no longer needed.
Remove CMAKE_BUILD_TYPE from msvc CI target as it is a Multi-Config generator and ignores it

### test as a target
Update test/CmakeLists.txt to use target_link_libraries to pull in required things from godot-cpp target
Add EXCLUDE_FROM_ALL to godot-cpp-test so that it isn't built as part of the 'all' target

== godot-cpp-test ==
updated ci to build the godot-cpp-test target from the root directory using cmake
Adding the LANGUAGES CXX to the test project maintains the existing behavior of not checking for C compiler during configure.
Removed majority of the cmake code as it was duplicating things that propagate as transient dependency from godot-cpp
  • Loading branch information
enetheru committed Sep 23, 2024
1 parent 355e16a commit cc25ef6
Show file tree
Hide file tree
Showing 6 changed files with 267 additions and 284 deletions.
17 changes: 7 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,11 @@ jobs:
- name: Build godot-cpp
run: |
cmake -DCMAKE_BUILD_TYPE=Release .
make -j $(nproc) VERBOSE=1
cmake --build . --verbose -j $(nproc) -t godot-cpp
- name: Build test GDExtension library
run: |
cd test && cmake -DCMAKE_BUILD_TYPE=Release -DGODOT_HEADERS_PATH="../godot-headers" -DCPP_BINDINGS_PATH=".." .
make -j $(nproc) VERBOSE=1
cmake --build . --verbose -j $(nproc) -t godot-cpp-test
linux-cmake-ninja:
name: 🐧 Build (Linux, GCC, CMake Ninja)
Expand All @@ -245,12 +244,11 @@ jobs:
- name: Build godot-cpp
run: |
cmake -DCMAKE_BUILD_TYPE=Release -GNinja .
cmake --build . -j $(nproc) --verbose
cmake --build . --verbose -j $(nproc) -t godot-cpp
- name: Build test GDExtension library
run: |
cd test && cmake -DCMAKE_BUILD_TYPE=Release -DGODOT_HEADERS_PATH="../godot-headers" -DCPP_BINDINGS_PATH=".." -GNinja .
cmake --build . -j $(nproc) --verbose
cmake --build . --verbose -j $(nproc) -t godot-cpp-test
windows-msvc-cmake:
name: 🏁 Build (Windows, MSVC, CMake)
Expand All @@ -263,10 +261,9 @@ jobs:

- name: Build godot-cpp
run: |
cmake -DCMAKE_BUILD_TYPE=Release -G"Visual Studio 16 2019" .
cmake --build . --verbose
cmake -G"Visual Studio 16 2019" .
cmake --build . --verbose -t godot-cpp
- name: Build test GDExtension library
run: |
cd test && cmake -DCMAKE_BUILD_TYPE=Release -DGODOT_HEADERS_PATH="../godot-headers" -DCPP_BINDINGS_PATH=".." -G"Visual Studio 16 2019" .
cmake --build . --verbose
cmake --build . --verbose -t godot-cpp-test
17 changes: 9 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
cmake_minimum_required(VERSION 3.13)
project(godot-cpp LANGUAGES CXX)

# Configure CMake
# https://discourse.cmake.org/t/how-do-i-remove-compile-options-from-target/5965
# https://stackoverflow.com/questions/74426638/how-to-remove-rtc1-from-specific-target-or-file-in-cmake
if(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
if(NOT CMAKE_BUILD_TYPE MATCHES Debug)
STRING(REGEX REPLACE "/RTC(su|[1su])" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
string(REPLACE "/RTC1" "" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
endif ()
# Get Python
find_package(Python3 3.4 REQUIRED) # pathlib should be present

if( CMAKE_C_COMPILER)
#Silence warning from unused CMAKE_C_COMPILER from toolchain
endif ()

# Main Library
include( ${PROJECT_SOURCE_DIR}/cmake/godotcpp.cmake )

# I know this doesn't look like a typical CMakeLists.txt, but as we are
Expand All @@ -22,3 +20,6 @@ include( ${PROJECT_SOURCE_DIR}/cmake/godotcpp.cmake )
godotcpp_options()

godotcpp_generate()

# Test Example
add_subdirectory( test )
137 changes: 90 additions & 47 deletions cmake/common_compiler_flags.cmake
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
# Add warnings based on compiler & version
# Set some helper variables for readability
set( compiler_less_than_v8 "$<VERSION_LESS:$<CXX_COMPILER_VERSION>,8>" )
set( compiler_greater_than_or_equal_v9 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,9>" )
set( compiler_greater_than_or_equal_v11 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,11>" )
set( compiler_less_than_v11 "$<VERSION_LESS:$<CXX_COMPILER_VERSION>,11>" )
set( compiler_greater_than_or_equal_v12 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,12>" )
#Generator Expression Helpers
set( IS_CLANG "$<OR:$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:Clang>>" )
set( IS_GNU "$<CXX_COMPILER_ID:GNU>" )
set( IS_MSVC "$<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( WARNING_AS_ERROR "$<BOOL:${GODOT_WARNING_AS_ERROR}>")

set( NOT_RELEASE "$<NOT:$<CONFIG:Release>>")

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

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

target_compile_features(${PROJECT_NAME}
PUBLIC
cxx_std_17
)

# These compiler options reflect what is in godot/SConstruct.
target_compile_options( ${PROJECT_NAME} PRIVATE
target_compile_options( ${PROJECT_NAME}
PUBLIC
# MSVC only
$<${compiler_is_msvc}:
$<${IS_MSVC}:
/W4

# Disable warnings which we don't plan to fix.
Expand All @@ -23,72 +41,97 @@ target_compile_options( ${PROJECT_NAME} PRIVATE
/wd4514 # C4514 (unreferenced inline function has been removed)
/wd4714 # C4714 (function marked as __forceinline not inlined)
/wd4820 # C4820 (padding added after construct)
$<${WARNING_AS_ERROR}:/WX>
>
PRIVATE

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

$<${WARNING_AS_ERROR}:-Werror>
>

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

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

# Bogus warning fixed in 8+.
-Wno-strict-overflow
>
$<$<AND:${compiler_is_gnu},${compiler_greater_than_or_equal_v9}>:
-Wattribute-alias=2
>
$<$<AND:${compiler_is_gnu},${compiler_greater_than_or_equal_v11}>:
$<${GNU_LT_V8}:-Wno-strict-overflow>

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

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

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

# False positives in our error macros, see GH-58747.
-Wno-return-type
$<${GNU_GE_V12}:-Wno-return-type>
>
PUBLIC
$<${IS_MSVC}:
/utf-8
$<IF:$<CONFIG:Debug>,/MDd,/MD /O2>
$<$<NOT:${DISABLE_EXCEPTIONS}>:/EHsc>
>

$<$<OR:${IS_CLANG},${IS_GNU}>:
$<${DISABLE_EXCEPTIONS}:-fno-exceptions>

$<IF:$<CONFIG:Debug>,-fno-omit-frame-pointer -O0 -g,-O3>
>
$<${IS_GNU}:$<${HOT_RELOAD}:-fno-gnu-unique>>
)

target_compile_definitions(${PROJECT_NAME}
PUBLIC
GDEXTENSION

$<$<BOOL:${WIN32}>:WINDOWS_ENABLED>

$<${IS_MSVC}:
TYPED_METHOD_BIND
NOMINMAX
$<${DISABLE_EXCEPTIONS}:_HAS_EXCEPTIONS=0>
>

$<IF:$<CONFIG:Debug>,DEBUG_ENABLED DEBUG_METHODS_ENABLED,NDEBUG>

$<${HOT_RELOAD}:HOT_RELOAD_ENABLED>

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

# Treat warnings as errors
function( set_warning_as_error )
message( STATUS "[${PROJECT_NAME}] Treating warnings as errors")
if ( CMAKE_VERSION VERSION_GREATER_EQUAL "3.24" )
set_target_properties( ${PROJECT_NAME}
PROPERTIES
COMPILE_WARNING_AS_ERROR ON
)
else()
target_compile_options( ${PROJECT_NAME}
PRIVATE
$<${compiler_is_msvc}:/WX>
$<$<OR:${compiler_is_clang},${compiler_is_gnu}>:-Werror>
)
endif()
endfunction()

if ( GODOT_WARNING_AS_ERROR )
set_warning_as_error()
endif()
target_link_options(${PROJECT_NAME} PRIVATE
$<$<OR:${IS_CLANG},${IS_GNU}>:
-static-libgcc
-static-libstdc++
>
# reading up on RPATH this is only relevant for unix based systems
# https://stackoverflow.com/questions/107888/is-there-a-windows-msvc-equivalent-to-the-rpath-linker-flag
# Is probably worth putting it behind another guard in the future.
# It appears that gcc silently ignores it on windows
# It's also probably unnecessary to be explicitly stated as CMake has a target property for it.
$<${IS_GNU}:
-Wl,-R,'$$ORIGIN'
>
)
Loading

0 comments on commit cc25ef6

Please sign in to comment.