Skip to content

Commit

Permalink
Modernise Existing CMakeLists.txt
Browse files Browse the repository at this point in the history
- Added to .gitignore CMakeUserPresets.json

Configuration:
- Silenced warning for unused CMAKE_C_COMPILER when specified in toolchain
- Moved find package for python to root CMakeLists.txt
- Changed python command to use single quotes to make build output log more legible.
- Added GODOT_DEV_BUILD to allow differentiation of debug or Release builds.

== targets  ==
Changed godot-cpp-test to be a target
Split godot-cpp into template_release, template_debug, editor
Target Compile Options:
- Changed the MSVC warning flags to PUBLIC to propagate to consumers
- Audit MSVC compile options and verified either defaults, or match scons
- fixed msvc runtime target to be MT[d] like the scons build
separated msvc properties visually.
Target Link Options:
- prevent use of -static-libgcc on APPLE platform
- removed manual rpath origin from link options
Target Definitions:
- Turn on WINDOWS_ENABLED based on WIN32
Target properties:
- Added CXX_STANDARD 17
- Added CXX_VISIBILITY_PRESET
- Added LINK_SEARCH_START_STATIC
- Added LINK_SEARCH_END_STATIC
- New cmake/sources.cmake to collect all the pre-existing source files, because globing is evil.
- Made 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

== godot-cpp-test ==
Add EXCLUDE_FROM_ALL to godot-cpp-test so that it isn't built as part of the 'all' target
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

### Documentation
Updated with new information
Added Emscripten example
Added Android example
  • Loading branch information
enetheru committed Oct 28, 2024
1 parent a98d41f commit a75b2cc
Show file tree
Hide file tree
Showing 8 changed files with 578 additions and 422 deletions.
46 changes: 8 additions & 38 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -204,30 +204,6 @@ jobs:
path: ${{ matrix.artifact-path }}
if-no-files-found: error

linux-cmake:
name: 🐧 Build (Linux, GCC, CMake)
runs-on: ubuntu-20.04
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: recursive

- name: Install dependencies
run: |
sudo apt-get update -qq
sudo apt-get install -qqq build-essential pkg-config cmake
- name: Build godot-cpp
run: |
cmake -DCMAKE_BUILD_TYPE=Release .
make -j $(nproc) VERBOSE=1
- 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
linux-cmake-ninja:
name: 🐧 Build (Linux, GCC, CMake Ninja)
runs-on: ubuntu-20.04
Expand All @@ -242,15 +218,12 @@ jobs:
sudo apt-get update -qq
sudo apt-get install -qqq build-essential pkg-config cmake ninja-build
- name: Build godot-cpp
run: |
cmake -DCMAKE_BUILD_TYPE=Release -GNinja .
cmake --build . -j $(nproc) --verbose
- 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
mkdir cmake-build
cd cmake-build
cmake ../
cmake --build . --verbose -j $(nproc) -t godot-cpp-test
windows-msvc-cmake:
name: 🏁 Build (Windows, MSVC, CMake)
Expand All @@ -261,12 +234,9 @@ jobs:
with:
submodules: recursive

- name: Build godot-cpp
run: |
cmake -DCMAKE_BUILD_TYPE=Release -G"Visual Studio 16 2019" .
cmake --build . --verbose --config Release
- 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 --config Release
mkdir cmake-build
cd cmake-build
cmake ../
cmake --build . --verbose -t godot-cpp-test --config Release
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,6 @@ venv
# Clion Configuration
.idea/
cmake-build-*

# CMake related
CMakeUserPresets.json
50 changes: 33 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
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 ()
endif ()

include( ${PROJECT_SOURCE_DIR}/cmake/godotcpp.cmake )

# I know this doesn't look like a typical CMakeLists.txt, but as we are
# attempting mostly feature parity with SCons, and easy maintenance, the closer
# the two build systems look the easier they will be to keep in lockstep.
# As we are attempting to maintain feature parity, and ease of maintenance,
# these CMake scripts are built to resemble the structure of the SCons build system.
# The closer the two build systems look the easier they will be to maintain.

# The typical target definitions are in ${PROJECT_SOURCE_DIR}/cmake/godotcpp.cmake
# include pulls in the code from godotcpp.cmake
# the equivalent in scons:
# cpp_tool = Tool("godotcpp", toolpath=["tools"])
include( ${CMAKE_SOURCE_DIR}/cmake/godotcpp.cmake )

godotcpp_options()

# godot-cpp targets three main configurations, editor, template_release and template_debug.
# These are all built in "Release" mode unless GODOT_DEV_BUILD is enabled, then the build type is "Debug".
# If using MSBuild the build type is 'Debug' unless specified on the command line like so:
# cmake --build . --config Release
get_property( IS_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG )
if( NOT IS_MULTI_CONFIG AND NOT CMAKE_BUILD_TYPE )
if( GODOT_DEV_BUILD )
set( CMAKE_BUILD_TYPE Debug )
else ()
set( CMAKE_BUILD_TYPE Release )
endif ()
endif ()

# Get Python
find_package(Python3 3.4 REQUIRED) # pathlib should be present

# Define our project.
project(godot-cpp
VERSION 4.4
DESCRIPTION ""
HOMEPAGE_URL "https://github.com/godotengine/godot-cpp"
LANGUAGES CXX)

godotcpp_generate()

# Test Example
add_subdirectory( test )
119 changes: 72 additions & 47 deletions cmake/common_compiler_flags.cmake
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
# 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( HOT_RELOAD-UNSET "$<STREQUAL:${GODOT_USE_HOT_RELOAD},>")

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


function( configure_target TARGET_NAME )
set( IS_RELEASE "$<STREQUAL:${TARGET_NAME},template_release>")

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

target_compile_features(${TARGET_NAME}
PUBLIC
cxx_std_17
)

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

# Disable warnings which we don't plan to fix.
Expand All @@ -26,69 +46,74 @@ target_compile_options( ${PROJECT_NAME} 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
>
>

# 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>
>

$<${IS_MSVC}:
/utf-8
$<IF:$<CONFIG:Debug>,/MTd,/MT>
>

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

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

# 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()
target_compile_definitions(${TARGET_NAME}
PUBLIC
GDEXTENSION

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

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

$<${HOT_RELOAD}:HOT_RELOAD_ENABLED>

if ( GODOT_WARNING_AS_ERROR )
set_warning_as_error()
endif()
$<$<STREQUAL:${GODOT_PRECISION},double>:REAL_T_IS_DOUBLE>
)

endfunction()
Loading

0 comments on commit a75b2cc

Please sign in to comment.