Skip to content

Commit

Permalink
new cmake/sources.cmake to collect all the pre-existing source files,…
Browse files Browse the repository at this point in the history
… because globing is evil.

Moved all flags to generator expressions
Moved generator expression helpers to common_compiler_flags.cmake
Renamed helper variables to shorten to ease readability
Moved find package for python to root CMakeLists.txt
Silenced warning for unused CMAKE_C_COMPILER
Update test/CmakeLists.txt to use target_link_libraries to pull in required things from godot-cpp target
updated ci to build the godot-cpp-test target from the root directory using cmake
Changed the visibility of GENERATED_FILES_LIST to PRIVATE
Changed the MSVC warning flags to PUBLIC to hide warning in consumers
changed godot-cpp compile feature cxx_std_17 to PUBLIC so as to propagate to consumers
  • Loading branch information
enetheru committed Sep 19, 2024
1 parent 355e16a commit 461fa63
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 268 deletions.
15 changes: 6 additions & 9 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 @@ -264,9 +262,8 @@ jobs:
- name: Build godot-cpp
run: |
cmake -DCMAKE_BUILD_TYPE=Release -G"Visual Studio 16 2019" .
cmake --build . --verbose
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
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
cmake_minimum_required(VERSION 3.13)
project(godot-cpp LANGUAGES CXX)

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

# 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
Expand All @@ -11,6 +14,11 @@ if(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC)
endif ()
endif ()

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 +30,6 @@ include( ${PROJECT_SOURCE_DIR}/cmake/godotcpp.cmake )
godotcpp_options()

godotcpp_generate()

# Test Example
add_subdirectory( test )
124 changes: 77 additions & 47 deletions cmake/common_compiler_flags.cmake
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
# 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( HOT_RELOAD "$<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 +36,89 @@ 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>>
)

# 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_compile_definitions(${PROJECT_NAME}
PUBLIC
GDEXTENSION

$<${IS_MSVC}:
WINDOWS_ENABLED
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>
)

target_link_options(${PROJECT_NAME} PRIVATE
$<$<OR:${IS_CLANG},${IS_GNU}>:
-static-libgcc
-static-libstdc++
-Wl,-R,'$$ORIGIN'
>
)
104 changes: 17 additions & 87 deletions cmake/godotcpp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ function( godotcpp_options )
set(GODOT_USE_HOT_RELOAD "" CACHE BOOL
"Enable the extra accounting required to support hot reload. (ON|OFF)")

# Disable exception handling. Godot doesn't use exceptions anywhere, and this
# saves around 20% of binary size and very significant build time (GH-80513).
option(GODOT_DISABLE_EXCEPTIONS "Force disabling exception handling code (ON|OFF)" ON )

set( GODOT_SYMBOL_VISIBILITY "hidden" CACHE STRING
Expand All @@ -54,11 +56,7 @@ endfunction()


function( godotcpp_generate )
# Set some helper variables for readability
set( compiler_is_clang "$<OR:$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:Clang>>" )
set( compiler_is_gnu "$<CXX_COMPILER_ID:GNU>" )
set( compiler_is_msvc "$<CXX_COMPILER_ID:MSVC>" )

### Configure variables
# CXX_VISIBILITY_PRESET supported values are: default, hidden, protected, and internal
# which is inline with the gcc -fvisibility=
# https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html
Expand All @@ -78,6 +76,7 @@ function( godotcpp_generate )
set(GODOT_USE_HOT_RELOAD ON)
endif()

### Generate Bindings
if(NOT DEFINED BITS)
set(BITS 32)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
Expand All @@ -91,47 +90,7 @@ function( godotcpp_generate )
set(GODOT_GDEXTENSION_API_FILE "${GODOT_CUSTOM_API_FILE}")
endif()

if ("${GODOT_PRECISION}" STREQUAL "double")
add_definitions(-DREAL_T_IS_DOUBLE)
endif()

set( GODOT_COMPILE_FLAGS )

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
# using Visual Studio C++
set(GODOT_COMPILE_FLAGS "/utf-8") # /GF /MP

if(CMAKE_BUILD_TYPE MATCHES Debug)
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /MDd") # /Od /RTC1 /Zi
else()
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /MD /O2") # /Oy /GL /Gy
endif(CMAKE_BUILD_TYPE MATCHES Debug)

add_definitions(-DNOMINMAX)
else() # GCC/Clang
if(CMAKE_BUILD_TYPE MATCHES Debug)
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -fno-omit-frame-pointer -O0 -g")
else()
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -O3")
endif(CMAKE_BUILD_TYPE MATCHES Debug)
endif()

# Disable exception handling. Godot doesn't use exceptions anywhere, and this
# saves around 20% of binary size and very significant build time (GH-80513).
if (GODOT_DISABLE_EXCEPTIONS)
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -D_HAS_EXCEPTIONS=0")
else()
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -fno-exceptions")
endif()
else()
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /EHsc")
endif()
endif()

# Generate source from the bindings file
find_package(Python3 3.4 REQUIRED) # pathlib should be present
if(GODOT_GENERATE_TEMPLATE_GET_NODE)
set(GENERATE_BINDING_PARAMETERS "True")
else()
Expand All @@ -153,48 +112,18 @@ function( godotcpp_generate )
COMMENT "Generating bindings"
)

# Get Sources
# As this cmake file was added using 'include(godotcpp)' from the root CMakeLists.txt,
# the ${CMAKE_CURRENT_SOURCE_DIR} is still the root dir.
file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS src/*.c**)
file(GLOB_RECURSE HEADERS CONFIGURE_DEPENDS include/*.h**)

# Define our godot-cpp library
add_library(${PROJECT_NAME} STATIC
${SOURCES}
${HEADERS}
${GENERATED_FILES_LIST}
)
### Define our godot-cpp library
add_library(${PROJECT_NAME} STATIC )
add_library(godot::cpp ALIAS ${PROJECT_NAME})

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

target_compile_features(${PROJECT_NAME}
# Get Sources
include( cmake/sources.cmake )
target_sources( ${PROJECT_NAME}
PUBLIC
${GODOTCPP_SOURCES}
${GODOTCPP_HEADERS}
PRIVATE
cxx_std_17
)

if(GODOT_USE_HOT_RELOAD)
target_compile_definitions(${PROJECT_NAME} PUBLIC HOT_RELOAD_ENABLED)
target_compile_options(${PROJECT_NAME} PUBLIC $<${compiler_is_gnu}:-fno-gnu-unique>)
endif()

target_compile_definitions(${PROJECT_NAME} PUBLIC
$<$<CONFIG:Debug>:
DEBUG_ENABLED
DEBUG_METHODS_ENABLED
>
$<${compiler_is_msvc}:
TYPED_METHOD_BIND
>
)

target_link_options(${PROJECT_NAME} PRIVATE
$<$<NOT:${compiler_is_msvc}>:
-static-libgcc
-static-libstdc++
-Wl,-R,'$$ORIGIN'
>
${GENERATED_FILES_LIST}
)

# Optionally mark headers as SYSTEM
Expand All @@ -209,10 +138,10 @@ function( godotcpp_generate )
${GODOT_GDEXTENSION_DIR}
)

# Add the compile flags
set_property(TARGET ${PROJECT_NAME} APPEND_STRING PROPERTY COMPILE_FLAGS ${GODOT_COMPILE_FLAGS})
include(${PROJECT_SOURCE_DIR}/cmake/common_compiler_flags.cmake)


# Create the correct name (godot.os.build_type.system_bits)
### Create the correct name (godot.os.build_type.system_bits)
string(TOLOWER "${CMAKE_SYSTEM_NAME}" SYSTEM_NAME)
string(TOLOWER "${CMAKE_BUILD_TYPE}" BUILD_TYPE)

Expand All @@ -226,6 +155,7 @@ function( godotcpp_generate )
set(OUTPUT_NAME "godot-cpp.${SYSTEM_NAME}.${BUILD_TYPE}.${BITS}")
endif()


set_target_properties(${PROJECT_NAME}
PROPERTIES
CXX_EXTENSIONS OFF
Expand Down
Loading

0 comments on commit 461fa63

Please sign in to comment.