diff --git a/CMakeLists.txt b/CMakeLists.txt index ff77368ba6..866783e73a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 @@ -11,6 +14,10 @@ if(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC) endif () endif () +if( CMAKE_C_COMPILER) + #Silence warning from unused CMAKE_C_COMPILER from toolchain +endif () + include( ${PROJECT_SOURCE_DIR}/cmake/godotcpp.cmake ) # I know this doesn't look like a typical CMakeLists.txt, but as we are diff --git a/cmake/common_compiler_flags.cmake b/cmake/common_compiler_flags.cmake index 94556415b1..29b17f2c68 100644 --- a/cmake/common_compiler_flags.cmake +++ b/cmake/common_compiler_flags.cmake @@ -1,15 +1,28 @@ -# Add warnings based on compiler & version -# Set some helper variables for readability -set( compiler_less_than_v8 "$,8>" ) -set( compiler_greater_than_or_equal_v9 "$,9>" ) -set( compiler_greater_than_or_equal_v11 "$,11>" ) -set( compiler_less_than_v11 "$,11>" ) -set( compiler_greater_than_or_equal_v12 "$,12>" ) +#Generator Expression Helpers +set( IS_CLANG "$,$>" ) +set( IS_GNU "$" ) +set( IS_MSVC "$" ) + +set( GNU_LT_V8 "$,8>" ) +set( GNU_GE_V9 "$,9>" ) +set( GNU_GT_V11 "$,11>" ) +set( GNU_LT_V11 "$,11>" ) +set( GNU_GE_V12 "$,12>" ) + +set( WARNING_AS_ERROR "$") +set( HOT_RELOAD "$") +set( DISABLE_EXCEPTIONS "$") + +target_compile_features(${PROJECT_NAME} + PRIVATE + cxx_std_17 +) # These compiler options reflect what is in godot/SConstruct. -target_compile_options( ${PROJECT_NAME} PRIVATE +target_compile_options( ${PROJECT_NAME} +PRIVATE # MSVC only - $<${compiler_is_msvc}: + $<${IS_MSVC}: /W4 # Disable warnings which we don't plan to fix. @@ -23,26 +36,29 @@ 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> > # Clang and GNU common options - $<$: + $<$: -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 @@ -50,45 +66,58 @@ target_compile_options( ${PROJECT_NAME} PRIVATE -Wplacement-new=1 -Wshadow-local -Wstringop-overflow=4 - > - $<$: + # Bogus warning fixed in 8+. - -Wno-strict-overflow - > - $<$: - -Wattribute-alias=2 - > - $<$: + $<${GNU_LT_V8}:-Wno-strict-overflow> + + $<${GNU_GE_V9}:-Wattribute-alias=2> + # Broke on MethodBind templates before GCC 11. - -Wlogical-op - > - $<$: + $<${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 - > - $<$: + $<${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 + $,/MDd,/MD /O2> + $<$:/EHsc> > + + $<$: + $<${DISABLE_EXCEPTIONS}:-fno-exceptions> + + $,-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> - $<$:-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> + > + + $,DEBUG_ENABLED DEBUG_METHODS_ENABLED,NDEBUG> + + $<${HOT_RELOAD}:HOT_RELOAD_ENABLED> + + $<$:REAL_T_IS_DOUBLE> +) + +target_link_options(${PROJECT_NAME} PRIVATE + $<$: + -static-libgcc + -static-libstdc++ + -Wl,-R,'$$ORIGIN' + > +) diff --git a/cmake/godotcpp.cmake b/cmake/godotcpp.cmake index a5c6677964..e781a6a027 100644 --- a/cmake/godotcpp.cmake +++ b/cmake/godotcpp.cmake @@ -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 @@ -54,11 +56,7 @@ endfunction() function( godotcpp_generate ) - # Set some helper variables for readability - set( compiler_is_clang "$,$>" ) - set( compiler_is_gnu "$" ) - set( compiler_is_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 @@ -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) @@ -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() @@ -153,48 +112,17 @@ 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} - 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 - $<$: - DEBUG_ENABLED - DEBUG_METHODS_ENABLED - > - $<${compiler_is_msvc}: - TYPED_METHOD_BIND - > - ) - - target_link_options(${PROJECT_NAME} PRIVATE - $<$: - -static-libgcc - -static-libstdc++ - -Wl,-R,'$$ORIGIN' - > + # Get Sources + include( cmake/sources.cmake ) + target_sources( ${PROJECT_NAME} + PUBLIC + ${GODOTCPP_SOURCES} + ${GODOTCPP_HEADERS} + ${GENERATED_FILES_LIST} ) # Optionally mark headers as SYSTEM @@ -209,10 +137,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) @@ -226,6 +154,7 @@ function( godotcpp_generate ) set(OUTPUT_NAME "godot-cpp.${SYSTEM_NAME}.${BUILD_TYPE}.${BITS}") endif() + set_target_properties(${PROJECT_NAME} PROPERTIES CXX_EXTENSIONS OFF diff --git a/cmake/sources.cmake b/cmake/sources.cmake new file mode 100644 index 0000000000..939f17f437 --- /dev/null +++ b/cmake/sources.cmake @@ -0,0 +1,96 @@ +set( GODOTCPP_SOURCES + src/godot.cpp + src/classes/editor_plugin_registration.cpp + src/classes/low_level.cpp + src/classes/wrapped.cpp + src/core/class_db.cpp + src/core/error_macros.cpp + src/core/memory.cpp + src/core/method_bind.cpp + src/core/object.cpp + src/variant/aabb.cpp + src/variant/basis.cpp + src/variant/callable_custom.cpp + src/variant/callable_method_pointer.cpp + src/variant/char_string.cpp + src/variant/color.cpp + src/variant/packed_arrays.cpp + src/variant/plane.cpp + src/variant/projection.cpp + src/variant/quaternion.cpp + src/variant/rect2.cpp + src/variant/rect2i.cpp + src/variant/transform2d.cpp + src/variant/transform3d.cpp + src/variant/variant.cpp + src/variant/vector2.cpp + src/variant/vector2i.cpp + src/variant/vector3.cpp + src/variant/vector3i.cpp + src/variant/vector4.cpp + src/variant/vector4i.cpp +) +set( GODOTCPP_HEADERS + include/godot_cpp/godot.hpp + include/godot_cpp/classes/editor_plugin_registration.hpp + include/godot_cpp/classes/ref.hpp + include/godot_cpp/classes/wrapped.hpp + include/godot_cpp/core/binder_common.hpp + include/godot_cpp/core/builtin_ptrcall.hpp + include/godot_cpp/core/class_db.hpp + include/godot_cpp/core/defs.hpp + include/godot_cpp/core/engine_ptrcall.hpp + include/godot_cpp/core/error_macros.hpp + include/godot_cpp/core/math.hpp + include/godot_cpp/core/memory.hpp + include/godot_cpp/core/method_bind.hpp + include/godot_cpp/core/method_ptrcall.hpp + include/godot_cpp/core/mutex_lock.hpp + include/godot_cpp/core/object.hpp + include/godot_cpp/core/object_id.hpp + include/godot_cpp/core/property_info.hpp + include/godot_cpp/core/type_info.hpp + include/godot_cpp/templates/cowdata.hpp + include/godot_cpp/templates/hash_map.hpp + include/godot_cpp/templates/hash_set.hpp + include/godot_cpp/templates/hashfuncs.hpp + include/godot_cpp/templates/list.hpp + include/godot_cpp/templates/local_vector.hpp + include/godot_cpp/templates/pair.hpp + include/godot_cpp/templates/rb_map.hpp + include/godot_cpp/templates/rb_set.hpp + include/godot_cpp/templates/rid_owner.hpp + include/godot_cpp/templates/safe_refcount.hpp + include/godot_cpp/templates/search_array.hpp + include/godot_cpp/templates/self_list.hpp + include/godot_cpp/templates/sort_array.hpp + include/godot_cpp/templates/spin_lock.hpp + include/godot_cpp/templates/thread_work_pool.hpp + include/godot_cpp/templates/vector.hpp + include/godot_cpp/templates/vmap.hpp + include/godot_cpp/templates/vset.hpp + include/godot_cpp/variant/aabb.hpp + include/godot_cpp/variant/array_helpers.hpp + include/godot_cpp/variant/basis.hpp + include/godot_cpp/variant/callable_custom.hpp + include/godot_cpp/variant/callable_method_pointer.hpp + include/godot_cpp/variant/char_string.hpp + include/godot_cpp/variant/char_utils.hpp + include/godot_cpp/variant/color.hpp + include/godot_cpp/variant/color_names.inc.hpp + include/godot_cpp/variant/plane.hpp + include/godot_cpp/variant/projection.hpp + include/godot_cpp/variant/quaternion.hpp + include/godot_cpp/variant/rect2.hpp + include/godot_cpp/variant/rect2i.hpp + include/godot_cpp/variant/transform2d.hpp + include/godot_cpp/variant/transform3d.hpp + include/godot_cpp/variant/typed_array.hpp + include/godot_cpp/variant/variant.hpp + include/godot_cpp/variant/vector2.hpp + include/godot_cpp/variant/vector2i.hpp + include/godot_cpp/variant/vector3.hpp + include/godot_cpp/variant/vector3i.hpp + include/godot_cpp/variant/vector4.hpp + include/godot_cpp/variant/vector4i.hpp +)